Repository navigation
Expand file tree
/
Copy pathstats.c
More file actions
447 lines (386 loc) · 11.8 KB
/
Copy pathstats.c
File metadata and controls
447 lines (386 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright 2020 Morse Micro
* SPDX-License-Identifier: GPL-2.0-or-later OR LicenseRef-MorseMicroCommercial
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#ifndef MORSE_WIN_BUILD
#include <regex.h>
#endif
#include "portable_endian.h"
#include "command.h"
#include "elf_file.h"
#include "offchip_statistics.h"
#include "stats_format.h"
#include "utilities.h"
#include "transport/transport.h"
#include "mm_argtable3.h"
#ifndef MAX_PATH
#define MAX_PATH 1024
#endif
static struct
{
struct arg_lit *apps_core;
struct arg_lit *mac_core;
struct arg_lit *phy_core;
struct arg_lit *reset;
struct arg_lit *json_format;
struct arg_lit *pprint_format;
struct arg_str *filter_str;
struct arg_str *firmware_path;
} args;
/* Read and return a single word from a file.
* The result is always null terminated.
*/
static char *get_word_from_file(const char *path, char *word, size_t n)
{
FILE *infile = fopen(path, "r");
if (infile)
{
fgets(word, n, infile); /* fgets always null terminates */
fclose(infile);
return strip(word);
}
return NULL;
}
/* Get the firmware path from the driver debugfs file
* ...using phy name from sysfs file
* ...using wlan name from morsectrl struct
* The append it to the firmware path.
*
* If any of these steps fail, nothing is written to the firmware path.
*/
static void get_override_firmware_path(struct morsectrl *mors, char *firmware_full_path, size_t n)
{
char path_buf[MAX_PATH];
char content_buf[MAX_PATH];
const char *sysfs_path_fmt = "/sys/class/net/%s/phy80211/name";
const char *debugfs_path_fmt = "/sys/kernel/debug/ieee80211/%s/morse/firmware_path";
#ifndef CONFIG_ANDROID
const char *firmware_path_fmt = "/lib/firmware/%s";
#else
const char *firmware_path_fmt = "/vendor/firmware/%s";
#endif
const char *interface_name;
char *phy_name;
char *firmware_path;
interface_name = morsectrl_transport_get_ifname(mors->transport);
if (!interface_name)
{
interface_name = DEFAULT_INTERFACE_NAME;
}
snprintf(path_buf, sizeof(path_buf), sysfs_path_fmt, interface_name);
phy_name = get_word_from_file(path_buf, content_buf, sizeof(content_buf));
if (phy_name)
{
snprintf(path_buf, sizeof(path_buf), debugfs_path_fmt, phy_name);
firmware_path = get_word_from_file(path_buf, content_buf, sizeof(content_buf));
if (firmware_path)
{
snprintf(firmware_full_path, n, firmware_path_fmt, firmware_path);
}
}
}
static int load_offchip_statistics(struct morsectrl *mors, const char *filename)
{
FILE *infile;
#ifndef CONFIG_ANDROID
char firmware_path[MAX_PATH] = "/lib/firmware/morse/mm6108.bin";
#else
char firmware_path[MAX_PATH] = "/vendor/firmware/morse/mm6108.bin";
#endif
if (!filename)
{
filename = firmware_path;
get_override_firmware_path(mors, firmware_path, sizeof(firmware_path));
}
infile = fopen(filename, "rb");
if (infile)
{
uint8_t *buf = NULL;
load_file(infile, &buf);
if (buf)
{
morse_stats_load(&mors->stats, &mors->n_stats, buf);
free(buf);
}
fclose(infile);
}
else
{
mctrl_err("Error - could not open %s to read stats metadata\n", filename);
return -1;
}
return 0;
}
#ifndef MORSE_WIN_BUILD
static regex_t *filter_re = NULL;
static int filter_init(const char *filter_string)
{
int ret = 0;
#ifdef CONFIG_ANDROID
/* In android, regcomp returns an error "empty (sub)expression" as empty string is
* not considered as valid regex.
*/
if (filter_string[0] == '\0')
filter_string = "\\(\\)";
#endif
filter_re = malloc(sizeof(*filter_re));
ret = regcomp(filter_re, filter_string, 0);
if (ret)
{
size_t len = regerror(ret, filter_re, NULL, 0);
char *re_err_buf = malloc(len);
regerror(ret, filter_re, re_err_buf, len);
mctrl_err("Invalid filter string: %s\n", re_err_buf);
free(re_err_buf);
free(filter_re);
filter_re = NULL;
}
return ret;
}
static int filter_stat(const char *key)
{
return regexec(filter_re, key, 0, NULL, 0);
}
static void filter_deinit(void)
{
if (filter_re)
{
regfree(filter_re);
free(filter_re);
}
filter_re = NULL;
}
static const char *filter_help(void)
{
return "uses a regular expression";
}
#else
static const char *filter_str = NULL;
static int filter_init(const char *filter_string)
{
filter_str = filter_string;
return 0;
}
static int filter_stat(const char *key)
{
return strcmp(key, filter_str);
}
static void filter_deinit(void)
{
filter_str = NULL;
}
static const char *filter_help(void)
{
return "case sensitive, match from start of key";
}
#endif
int morsectrl_stats_cmd(struct morsectrl *mors, int cmd, int reset,
const char *filter_string, enum format_type format_val)
{
int ret = -1;
int resp_sz;
const struct format_table *table;
struct stats_response *resp;
struct morsectrl_transport_buff *cmd_tbuff =
morsectrl_transport_cmd_alloc(mors->transport, 0);
struct morsectrl_transport_buff *rsp_tbuff =
morsectrl_transport_resp_alloc(mors->transport, sizeof(*resp));
if (!cmd_tbuff || !rsp_tbuff)
goto exit;
if (filter_string)
{
ret = filter_init(filter_string);
if (ret)
goto exit;
}
resp = TBUFF_TO_RSP(rsp_tbuff, struct stats_response);
if (reset)
cmd += 1;
ret = morsectrl_send_command(mors->transport, cmd, cmd_tbuff, rsp_tbuff);
resp_sz = rsp_tbuff->data_len - sizeof(struct response);
if (ret)
{
/* Try the deprecated command */
ret = morsectrl_send_command(mors->transport, OLD_STATS_COMMAND_MASK & cmd,
cmd_tbuff, rsp_tbuff);
resp_sz = rsp_tbuff->data_len - sizeof(struct response);
if (!reset && !ret)
{
mctrl_print("%s", resp->stats);
}
goto exit;
}
if (!reset && !ret)
{
uint8_t *buf = (uint8_t *)resp->stats;
switch (format_val)
{
case FORMAT_REGULAR:
{
table = stats_format_regular_get_formatter_table();
break;
}
case FORMAT_JSON_PPRINT:
{
stats_format_json_set_pprint(true);
/* fall through */
}
case FORMAT_JSON:
{
table = stats_format_json_get_formatter_table();
break;
}
default:
ret = -1;
goto exit;
}
while (resp_sz > STATS_TLV_OVERHEAD )
{
stats_tlv_tag_t tag = *((stats_tlv_tag_t *)buf);
buf += sizeof(stats_tlv_tag_t);
stats_tlv_len_t len = le16toh(*((__force __le16 *)buf));
buf += sizeof(stats_tlv_len_t);
if ((len > resp_sz) || (len == 0))
{
mctrl_err("error: malformed TLV (tag %d/0x%x, len %u/0x%x, size %u)\n",
tag, tag, len, len, resp_sz);
break;
}
struct statistics_offchip_data *offchip = get_stats_offchip(mors, tag);
if (offchip)
{
uint32_t format = le32toh((__force __le32)offchip->format);
if ((format == MORSE_STATS_FMT_DEC) &&
!strncmp(offchip->type_str, "uint", 4))
{
format = MORSE_STATS_FMT_U_DEC;
}
if (!filter_string || !filter_stat(offchip->key))
{
if (format_val == FORMAT_JSON || format_val == FORMAT_JSON_PPRINT)
{
stats_format_json_init();
}
if (format > MORSE_STATS_FMT_LAST)
{
format = MORSE_STATS_FMT_LAST;
}
table->format_func[format]((const char *) offchip->key,
(const uint8_t *)buf, len);
}
}
else
{
mctrl_err("UNKNOWN KEY for tag %d: ", tag);
hexdump(buf, len);
mctrl_err("\n");
}
buf += len;
resp_sz -= (STATS_TLV_OVERHEAD + len);
}
}
exit:
filter_deinit();
morsectrl_transport_buff_free(cmd_tbuff);
morsectrl_transport_buff_free(rsp_tbuff);
return ret;
}
static void dump_stats_types(struct morsectrl *mors)
{
int ii;
mctrl_print("Stats types\n");
for (ii = 0; ii < mors->n_stats; ii++)
{
mctrl_print("Type: %s\n", mors->stats[ii].type_str);
mctrl_print("Name: %s\n", mors->stats[ii].name);
mctrl_print("Key: %s\n\n", mors->stats[ii].key);
}
}
int stats_init(struct morsectrl *mors, struct mm_argtable *mm_args)
{
MM_INIT_ARGTABLE(mm_args, "Read statistics from the chip",
args.apps_core = arg_lit0("a", NULL, "read statistics from the Apps core"),
args.mac_core = arg_lit0("m", NULL, "read statistics from the MAC core"),
args.phy_core = arg_lit0("u", NULL, "read statistics from the PHY core"),
args.reset = arg_lit0("r", NULL, "reset the statistics"),
args.json_format = arg_lit0("j", "json", "Format the statistics in JSON"),
args.pprint_format = arg_lit0("p", NULL,
"Format the statistics in human-readable JSON"),
args.filter_str = arg_str0("f", "filter", "<filter>", filter_help()),
args.firmware_path =
arg_str0("s", "firmware",
"<firmware>",
"Path to the firmware used to process the statistics"));
return 0;
}
int stats(struct morsectrl *mors, int argc, char *argv[])
{
int ret = 0;
bool reset = false, app_c = false, mac_c = false, uph_c = false;
enum format_type format = FORMAT_REGULAR;
ret = load_offchip_statistics(mors, args.firmware_path->count >
0 ? args.firmware_path->sval[0] : NULL);
if (ret)
{
goto exit_stats;
}
if (mors->debug)
dump_stats_types(mors);
app_c = (bool)(args.apps_core->count > 0);
mac_c = (bool)(args.mac_core->count > 0);
uph_c = (bool)(args.phy_core->count > 0);
/* If no core selected then enable all. */
if ((!app_c) && (!mac_c) && (!uph_c))
{
app_c = true;
mac_c = true;
uph_c = true;
}
if (args.json_format->count > 0)
format = FORMAT_JSON;
else if (args.pprint_format->count > 0)
format = FORMAT_JSON_PPRINT;
if (format == FORMAT_JSON)
{
mctrl_print("{");
}
else if (format == FORMAT_JSON_PPRINT)
{
mctrl_print("{\n");
}
reset = !!(args.reset->count);
if (app_c)
{
ret = morsectrl_stats_cmd(mors, MORSE_CMD_ID_HOST_STATS_LOG, reset,
args.filter_str->sval[0], format);
if (ret) goto exit_stats;
}
if (mac_c)
{
ret = morsectrl_stats_cmd(mors, MORSE_CMD_ID_MAC_STATS_LOG, reset,
args.filter_str->sval[0], format);
if (ret) goto exit_stats;
}
if (uph_c)
{
ret = morsectrl_stats_cmd(mors, MORSE_CMD_ID_UPHY_STATS_LOG, reset,
args.filter_str->sval[0], format);
if (ret) goto exit_stats;
}
if (format == FORMAT_JSON)
{
mctrl_print("}\n");
}
else if (format == FORMAT_JSON_PPRINT)
{
mctrl_print("\n}\n");
}
exit_stats:
return ret;
}
MM_CLI_HANDLER(stats, MM_INTF_REQUIRED, MM_DIRECT_CHIP_SUPPORTED);