2 * parse_util.c - halfhearted json parser
4 * Copyright (c) 2018 Cisco Systems and/or its affiliates
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <perfmon/perfmon.h>
19 #include <vppinfra/unix.h>
35 for (i = 0; i < vec_len (s); i++)
38 if (c >= 'A' && c <= 'Z')
46 perfmon_parse_table (perfmon_main_t * pm, char *path, char *table_name)
50 int state = STATE_START;
52 name_value_pair_t *nvp = 0;
53 name_value_pair_t **nvps = 0;
59 /* Create the name/value hash table in any case... */
60 ht = hash_create_string (0, sizeof (uword));
62 json_filename = format (0, "%s/%s%c", path, table_name, 0);
64 vlib_log_debug (pm->log_class, "Try to read perfmon events from %s",
67 error = unix_proc_file_contents ((char *) json_filename, &cp);
71 vlib_log_err (pm->log_class,
72 "Failed to read CPU-specific counter table");
73 vlib_log_err (pm->log_class,
74 "Please install the vpp-dev package and then:");
76 (pm->log_class, "cd %s; sudo tar Jxf PerfmonTables.tar.xz", path);
77 vlib_log_err (pm->log_class, "and restart vpp.");
79 vec_free (json_filename);
80 clib_error_report (error);
83 vlib_log_debug (pm->log_class, "Read OK, parse the event table...");
84 vec_free (json_filename);
92 while (*cp && *cp != '{' && *cp != '}' && *cp != ',')
97 /* Look for a new event */
103 clib_warning ("parse fail");
108 state = STATE_READ_NAME;
111 else if (*cp == '}') /* end of event */
113 /* Look for the "EventName" nvp */
114 for (i = 0; i < vec_len (nvps); i++)
117 if (!strncmp ((char *) nvp->name, "EventName", 9))
119 event_name = nvp->value;
124 for (i = 0; i < vec_len (nvps); i++)
126 vec_free (nvps[i]->name);
127 vec_free (nvps[i]->value);
134 event_name = downcase (event_name);
135 hash_set_mem (ht, event_name, nvps);
141 else if (*cp == ',') /* punctuation */
146 else /* should never happen... */
150 case STATE_READ_NAME:
151 vec_validate (nvp, 0);
153 while (*cp && *cp != '"')
163 while (*cp && *cp != '"')
176 state = STATE_READ_VALUE;
179 case STATE_READ_VALUE:
180 while (*cp && *cp != ':')
184 vec_free (nvp->name);
187 while (*cp && *cp != '"')
191 vec_free (nvp->name);
197 while (*cp && *cp != '"')
204 vec_free (nvp->name);
210 vec_add1 (nvps, nvp);
211 while (*cp && *cp != ',' && *cp != '}')
215 vec_free (nvp->name);
216 vec_free (nvp->value);
224 state = STATE_READ_NAME;
236 * fd.io coding-style-patch-verification: ON
239 * eval: (c-set-style "gnu")