Initial commit of vpp code.
[vpp.git] / vpp / api / gmon.c
1 /* 
2  * Copyright (c) 2012 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <netinet/in.h>
22 #include <signal.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/bitmap.h>
32 #include <vppinfra/fifo.h>
33 #include <vppinfra/time.h>
34 #include <vppinfra/mheap.h>
35 #include <vppinfra/heap.h>
36 #include <vppinfra/pool.h>
37 #include <vppinfra/format.h>
38
39 #include <vlib/vlib.h>
40 #include <vlib/unix/unix.h>
41
42 #include <svmdb.h>
43
44 typedef struct {
45     svmdb_client_t *svmdb_client;
46     f64 *vector_rate_ptr;
47     f64 *input_rate_ptr;
48     pid_t *vpef_pid_ptr;
49     vlib_main_t *vlib_main;
50 } gmon_main_t;
51
52 #if DPDK == 0
53 static inline u64 vnet_get_aggregate_rx_packets (void)
54 { return 0; }
55 #else
56 #include <vnet/vnet.h>
57 #include <vnet/devices/dpdk/dpdk.h>
58 #endif
59
60 gmon_main_t gmon_main;
61
62 static uword
63 gmon_process (vlib_main_t * vm,
64               vlib_node_runtime_t * rt,
65               vlib_frame_t * f)
66 {
67     f64 vector_rate;
68     u64 input_packets, last_input_packets;
69     f64 last_runtime, dt, now;
70     gmon_main_t *gm = &gmon_main;
71     pid_t vpefpid;
72
73     vpefpid = getpid();
74     *gm->vpef_pid_ptr = vpefpid;
75
76     last_runtime = 0.0; 
77     last_input_packets = 0;
78               
79     last_runtime = 0.0;
80     last_input_packets = 0;
81
82     while (1) {
83         vlib_process_suspend (vm, 5.0);
84         vector_rate = vlib_last_vector_length_per_node (vm);
85         *gm->vector_rate_ptr = vector_rate;
86         now = vlib_time_now(vm);
87         dt = now - last_runtime;
88         input_packets =  vnet_get_aggregate_rx_packets();
89         *gm->input_rate_ptr = (f64)(input_packets - last_input_packets) / dt;
90         last_runtime = now;
91         last_input_packets = input_packets;
92     }
93
94     return 0; /* not so much */
95 }
96
97 VLIB_REGISTER_NODE (gmon_process_node,static) = {
98     .function = gmon_process,
99     .type = VLIB_NODE_TYPE_PROCESS,
100     .name = "gmon-process",
101 };
102
103 static clib_error_t *
104 gmon_init (vlib_main_t *vm)
105 {
106     gmon_main_t *gm = &gmon_main;
107     pid_t *swp = 0;
108     f64 *v = 0;
109
110     gm->vlib_main = vm;
111     gm->svmdb_client = svmdb_map();
112
113     /* Find or create, set to zero */
114     vec_add1 (v, 0.0);
115     svmdb_local_set_vec_variable(gm->svmdb_client, 
116                                  "vlib_vector_rate", 
117                                  (char *)v, sizeof (*v));
118     vec_free(v);
119     vec_add1 (v, 0.0);
120     svmdb_local_set_vec_variable(gm->svmdb_client, 
121                                  "vnet_input_rate", 
122                                  (char *)v, sizeof (*v));
123     vec_free(v);
124
125     vec_add1 (swp, 0.0);
126     svmdb_local_set_vec_variable(gm->svmdb_client, 
127                                  "vpef_pid", 
128                                  (char *)swp, sizeof (*swp));
129     vec_free(swp);
130
131     /* the value cell will never move, so acquire a reference to it */
132     gm->vector_rate_ptr = 
133         svmdb_local_get_variable_reference (gm->svmdb_client,
134                                             SVMDB_NAMESPACE_VEC, 
135                                             "vlib_vector_rate");
136     gm->input_rate_ptr = 
137         svmdb_local_get_variable_reference (gm->svmdb_client,
138                                             SVMDB_NAMESPACE_VEC, 
139                                             "vnet_input_rate");
140     gm->vpef_pid_ptr = 
141         svmdb_local_get_variable_reference (gm->svmdb_client,
142                                             SVMDB_NAMESPACE_VEC, 
143                                             "vpef_pid");
144     return 0;
145 }
146
147 VLIB_INIT_FUNCTION (gmon_init);
148
149 static clib_error_t *gmon_exit (vlib_main_t *vm)
150 {
151     gmon_main_t *gm = &gmon_main;
152
153     if (gm->vector_rate_ptr) {
154         *gm->vector_rate_ptr = 0.0;
155         *gm->vpef_pid_ptr = 0;
156         *gm->input_rate_ptr = 0.0;
157         svmdb_unmap (gm->svmdb_client);
158     }
159     return 0;
160 }
161 VLIB_MAIN_LOOP_EXIT_FUNCTION (gmon_exit);