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 %s",
74 vlib_log_err (pm->log_class,
75 "Please install the vpp-dev package and then:");
77 (pm->log_class, "cd %s; sudo tar Jxf PerfmonTables.tar.xz", path);
78 vlib_log_err (pm->log_class, "and restart vpp.");
80 vec_free (json_filename);
81 clib_error_report (error);
84 vlib_log_debug (pm->log_class, "Read OK, parse the event table...");
85 vec_free (json_filename);
93 while (*cp && *cp != '{' && *cp != '}' && *cp != ',')
98 /* Look for a new event */
104 clib_warning ("parse fail");
109 state = STATE_READ_NAME;
112 else if (*cp == '}') /* end of event */
114 /* Look for the "EventName" nvp */
115 for (i = 0; i < vec_len (nvps); i++)
118 if (!strncmp ((char *) nvp->name, "EventName", 9))
120 event_name = nvp->value;
125 for (i = 0; i < vec_len (nvps); i++)
127 vec_free (nvps[i]->name);
128 vec_free (nvps[i]->value);
135 event_name = downcase (event_name);
136 hash_set_mem (ht, event_name, nvps);
142 else if (*cp == ',') /* punctuation */
147 else /* should never happen... */
151 case STATE_READ_NAME:
152 vec_validate (nvp, 0);
154 while (*cp && *cp != '"')
164 while (*cp && *cp != '"')
177 state = STATE_READ_VALUE;
180 case STATE_READ_VALUE:
181 while (*cp && *cp != ':')
185 vec_free (nvp->name);
188 while (*cp && *cp != '"')
192 vec_free (nvp->name);
198 while (*cp && *cp != '"')
205 vec_free (nvp->name);
211 vec_add1 (nvps, nvp);
212 while (*cp && *cp != ',' && *cp != '}')
216 vec_free (nvp->name);
217 vec_free (nvp->value);
225 state = STATE_READ_NAME;
237 * fd.io coding-style-patch-verification: ON
240 * eval: (c-set-style "gnu")