Rationalize metric names.
[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 #include <vlibapi/api.h>
39 #include <vlibmemory/api.h>
40
41 #include <vlib/vlib.h>
42 #include <vlib/unix/unix.h>
43
44 #include <svmdb.h>
45
46 typedef struct {
47     svmdb_client_t *svmdb_client;
48     f64 *vector_rate_ptr;
49     f64 *input_rate_ptr;
50     pid_t *vpef_pid_ptr;
51     vlib_main_t *vlib_main;
52 } gmon_main_t;
53
54 #if DPDK == 0
55 static inline u64 vnet_get_aggregate_rx_packets (void)
56 { return 0; }
57 #else
58 #include <vnet/vnet.h>
59 #include <vnet/devices/dpdk/dpdk.h>
60 #endif
61
62 gmon_main_t gmon_main;
63
64 static uword
65 gmon_process (vlib_main_t * vm,
66               vlib_node_runtime_t * rt,
67               vlib_frame_t * f)
68 {
69     f64 vector_rate;
70     u64 input_packets, last_input_packets;
71     f64 last_runtime, dt, now;
72     gmon_main_t *gm = &gmon_main;
73     pid_t vpefpid;
74
75     vpefpid = getpid();
76     *gm->vpef_pid_ptr = vpefpid;
77
78     last_runtime = 0.0; 
79     last_input_packets = 0;
80               
81     last_runtime = 0.0;
82     last_input_packets = 0;
83
84     while (1) {
85         vlib_process_suspend (vm, 5.0);
86         vector_rate = vlib_last_vector_length_per_node (vm);
87         *gm->vector_rate_ptr = vector_rate;
88         now = vlib_time_now(vm);
89         dt = now - last_runtime;
90         input_packets =  vnet_get_aggregate_rx_packets();
91         *gm->input_rate_ptr = (f64)(input_packets - last_input_packets) / dt;
92         last_runtime = now;
93         last_input_packets = input_packets;
94     }
95
96     return 0; /* not so much */
97 }
98
99 VLIB_REGISTER_NODE (gmon_process_node,static) = {
100     .function = gmon_process,
101     .type = VLIB_NODE_TYPE_PROCESS,
102     .name = "gmon-process",
103 };
104
105 static clib_error_t *
106 gmon_init (vlib_main_t *vm)
107 {
108     gmon_main_t *gm = &gmon_main;
109     api_main_t * am = &api_main;
110     pid_t *swp = 0;
111     f64 *v = 0;
112
113     gm->vlib_main = vm;
114     gm->svmdb_client = svmdb_map_chroot(am->root_path);
115
116     /* Find or create, set to zero */
117     vec_add1 (v, 0.0);
118     svmdb_local_set_vec_variable(gm->svmdb_client, 
119                                  "vpp_vector_rate", 
120                                  (char *)v, sizeof (*v));
121     vec_free(v);
122     vec_add1 (v, 0.0);
123     svmdb_local_set_vec_variable(gm->svmdb_client, 
124                                  "vpp_input_rate", 
125                                  (char *)v, sizeof (*v));
126     vec_free(v);
127
128     vec_add1 (swp, 0.0);
129     svmdb_local_set_vec_variable(gm->svmdb_client, 
130                                  "vpp_pid", 
131                                  (char *)swp, sizeof (*swp));
132     vec_free(swp);
133
134     /* the value cell will never move, so acquire a reference to it */
135     gm->vector_rate_ptr = 
136         svmdb_local_get_variable_reference (gm->svmdb_client,
137                                             SVMDB_NAMESPACE_VEC, 
138                                             "vpp_vector_rate");
139     gm->input_rate_ptr = 
140         svmdb_local_get_variable_reference (gm->svmdb_client,
141                                             SVMDB_NAMESPACE_VEC, 
142                                             "vpp_input_rate");
143     gm->vpef_pid_ptr = 
144         svmdb_local_get_variable_reference (gm->svmdb_client,
145                                             SVMDB_NAMESPACE_VEC, 
146                                             "vpp_pid");
147     return 0;
148 }
149
150 VLIB_INIT_FUNCTION (gmon_init);
151
152 static clib_error_t *gmon_exit (vlib_main_t *vm)
153 {
154     gmon_main_t *gm = &gmon_main;
155
156     if (gm->vector_rate_ptr) {
157         *gm->vector_rate_ptr = 0.0;
158         *gm->vpef_pid_ptr = 0;
159         *gm->input_rate_ptr = 0.0;
160         svmdb_unmap (gm->svmdb_client);
161     }
162     return 0;
163 }
164 VLIB_MAIN_LOOP_EXIT_FUNCTION (gmon_exit);