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 "Download from https://download.01.org/perfmon, "
75 "and install as %s", json_filename);
76 vec_free (json_filename);
77 clib_error_report (error);
80 vlib_log_debug (pm->log_class, "Read OK, parse the event table...");
81 vec_free (json_filename);
89 while (*cp && *cp != '{' && *cp != '}' && *cp != ',')
94 /* Look for a new event */
100 clib_warning ("parse fail");
105 state = STATE_READ_NAME;
108 else if (*cp == '}') /* end of event */
110 /* Look for the "EventName" nvp */
111 for (i = 0; i < vec_len (nvps); i++)
114 if (!strncmp ((char *) nvp->name, "EventName", 9))
116 event_name = nvp->value;
121 for (i = 0; i < vec_len (nvps); i++)
123 vec_free (nvps[i]->name);
124 vec_free (nvps[i]->value);
131 event_name = downcase (event_name);
132 hash_set_mem (ht, event_name, nvps);
138 else if (*cp == ',') /* punctuation */
143 else /* should never happen... */
147 case STATE_READ_NAME:
148 vec_validate (nvp, 0);
150 while (*cp && *cp != '"')
160 while (*cp && *cp != '"')
173 state = STATE_READ_VALUE;
176 case STATE_READ_VALUE:
177 while (*cp && *cp != ':')
181 vec_free (nvp->name);
184 while (*cp && *cp != '"')
188 vec_free (nvp->name);
194 while (*cp && *cp != '"')
201 vec_free (nvp->name);
207 vec_add1 (nvps, nvp);
208 while (*cp && *cp != ',' && *cp != '}')
212 vec_free (nvp->name);
213 vec_free (nvp->value);
221 state = STATE_READ_NAME;
233 * fd.io coding-style-patch-verification: ON
236 * eval: (c-set-style "gnu")