File: tracker.c

package info (click to toggle)
nvme-cli 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,664 kB
  • sloc: ansic: 80,727; sh: 2,257; python: 975; makefile: 70; ruby: 25
file content (506 lines) | stat: -rw-r--r-- 15,701 bytes parent folder | download
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// SPDX-License-Identifier: MIT
/*
 * Copyright (c) 2025 Solidigm.
 *
 * Author: leonardo.da.cunha@solidigm.com
 */
#include "common.h"
#include "telemetry-log.h"
#include "util/json.h"
#include "config.h"
#include "data-area.h"
#include "skht.h"

#define TRACKER_CHUNK_SIZE 4096
#define MAX_ARGS 31

#define ABLIST_BUFFER_SIGNATURE 0xab15ab15 // Valid ABLIST

/*
 * All structure information is derived from the JSON configuration file.
 * No static structure definitions are used.
 * The dynamic parsing relies entirely on the structure definitions
 * from the JSON configuration file.
 *
 * The expected JSON structure format from telemetry_log_structure_parse is:
 * { "struct_name": { "field1": value1, "field2": value2, ... } }
 */

// Get string for tracker level
static const char *get_level_string(uint32_t level)
{
	switch (level) {
	case 0: return "DEBUG";
	case 1: return "INFO";
	case 2: return "ERROR";
	case 3: return "CRITICAL";
	default: return "UNKNOWN";
	}
}

// Get tracker entry description from JSON config
static struct json_object *get_tracker_entry_info(struct json_object *config, uint32_t tracker_id)
{
	struct json_object *tracker_obj = NULL;
	struct json_object *entries_obj = NULL;
	struct json_object *entry_obj = NULL;
	char tracker_id_str[16];

	if (!config)
		return NULL;

	// Convert tracker_id to string for JSON key lookup
	snprintf(tracker_id_str, sizeof(tracker_id_str), "%u", tracker_id);

	// Navigate through JSON hierarchy
	if (!json_object_object_get_ex(config, "Tracker", &tracker_obj))
		return NULL;

	if (!json_object_object_get_ex(tracker_obj, "TrackerEntry", &entries_obj))
		return NULL;

	if (!json_object_object_get_ex(entries_obj, tracker_id_str, &entry_obj))
		return NULL;

	return entry_obj;
}

// Get argument description from tracker entry
static const char *get_arg_description(struct json_object *entry_obj, int arg_idx)
{
	char key_name[16];
	struct json_object *desc_obj = NULL;

	// Format key name for argument description
	snprintf(key_name, sizeof(key_name), "descArg%d", arg_idx);

	if (json_object_object_get_ex(entry_obj, key_name, &desc_obj))
		return json_object_get_string(desc_obj);

	return NULL;
}

static uint32_t get_struct_size_from_config(struct json_object *struct_def)
{
	struct json_object *size_bit_obj = NULL;
	uint32_t size_bits = 0;

	if (!struct_def) {
		SOLIDIGM_LOG_WARNING("Cannot get size from NULL structure definition");
		return 0;
	}

	// Get the sizeBit property from the structure definition
	if (json_object_object_get_ex(struct_def, "sizeBit", &size_bit_obj)) {
		size_bits = (uint32_t)json_object_get_uint64(size_bit_obj);
		// Convert from bits to bytes (divide by 8)
		return size_bits / 8;
	}

	SOLIDIGM_LOG_WARNING("Structure definition missing 'sizeBit' property");
	return 0;
}

static void parse_tracker_chunk_json(const struct telemetry_log *tl, uint32_t chunk_offset,
	int chunk_number, struct json_object *entries_array)
{
	struct json_object *ctx_obj = NULL;
	struct json_object *ablist_context_def = NULL;
	struct json_object *ablist_entry_def = NULL;
	struct json_object *tracker_entry_def = NULL;
	uint32_t offset = 0;
	uint32_t signature = 0;
	uint32_t entry_count = 0;
	uint32_t data_start_offset = 0;
	struct json_object *metadata_obj = NULL;
	struct json_object *config = tl->configuration;

	// Get structure definitions from the config - all are required for dynamic parsing
	if (!sldm_config_get_struct_by_key_version(config,
		"ablist_context_t", SKT_VER_MAJOR, SKT_VER_MINOR, &ablist_context_def) ||
		!sldm_config_get_struct_by_key_version(config,
		"ablist_entry_t", SKT_VER_MAJOR, SKT_VER_MINOR, &ablist_entry_def) ||
		!sldm_config_get_struct_by_key_version(config,
		"tracker_entry_t", SKT_VER_MAJOR, SKT_VER_MINOR, &tracker_entry_def)) {

		SOLIDIGM_LOG_WARNING(
			"Required structure definitions missing from config");
		return;
	}

	// Parse the ablist_context_t structure to get context information
	ctx_obj = json_object_new_object();
	metadata_obj = json_object_new_object();

	if (sldm_telemetry_structure_parse(tl, ablist_context_def, chunk_offset * 8,
				     ctx_obj, metadata_obj) != 0) {
		SOLIDIGM_LOG_WARNING("Failed to parse ablist_context_t using dynamic parsing");
		json_object_put(ctx_obj);
		json_object_put(metadata_obj);
		return;
	}

	// Extract fields from the nested object structure
	struct json_object *ablist_context_obj = NULL;

	if (json_object_object_get_ex(ctx_obj, "ablist_context_t", &ablist_context_obj)) {
		// Extract fields from the inner object
		struct json_object *sig_obj = NULL;
		struct json_object *start_offset_obj = NULL;
		struct json_object *count_obj = NULL;

		if (json_object_object_get_ex(ablist_context_obj, "signature", &sig_obj))
			signature = (uint32_t) json_object_get_int64(sig_obj);
		else
			SOLIDIGM_LOG_WARNING(
				"Failed to find 'signature' field in ablist_context_t");

		if (json_object_object_get_ex(ablist_context_obj, "data_start_offset",
					    &start_offset_obj))
			data_start_offset = json_object_get_int(start_offset_obj);
		else
			SOLIDIGM_LOG_WARNING(
				"Failed to find 'data_start_offset' field in ablist_context_t");

		if (json_object_object_get_ex(ablist_context_obj, "entry_count", &count_obj))
			entry_count = json_object_get_int(count_obj);
		else
			SOLIDIGM_LOG_WARNING(
				"Failed to find 'entry_count' field in ablist_context_t");
	} else {
		SOLIDIGM_LOG_WARNING("Nested 'ablist_context_t' object not found in parsed data");
		json_object_put(ctx_obj);
		json_object_put(metadata_obj);
		return;
	}

	json_object_put(ctx_obj);
	json_object_put(metadata_obj);

	// Validate chunk signature
	if (signature != ABLIST_BUFFER_SIGNATURE)
		return;

	offset = data_start_offset;

	// Process all entries in the chunk
	while (offset < TRACKER_CHUNK_SIZE && entry_count > 0) {
		uint32_t next_entry_index = 0;
		struct json_object *entry_json = json_object_new_object();
		struct json_object *entry_metadata = json_object_new_object();
		struct json_object *header_obj = NULL;
		struct json_object *data_obj = NULL;
		uint32_t hash = 0;
		struct json_object *tracker_entry_obj = NULL;
		struct json_object *ablist_entry_obj = NULL;
		struct json_object *next_obj = NULL;
		struct json_object *name_obj = NULL;
		struct json_object *file_obj = NULL;
		struct json_object *line_obj = NULL;
		struct json_object *hash_obj = NULL;
		struct json_object *time_obj = NULL;
		struct json_object *arm_id_obj = NULL;
		struct json_object *level_obj = NULL;
		struct json_object *group_obj = NULL;
		struct json_object *arg_count_obj = NULL;
		struct json_object *args_array = NULL;
		struct json_object *arg_obj;
		uint32_t arg_count = 0;
		// Get the size of ablist_entry_t from configuration
		uint32_t ablist_entry_size = get_struct_size_from_config(ablist_entry_def);

		if (ablist_entry_size == 0) {
			SOLIDIGM_LOG_WARNING(
				"Failed to get size of ablist_entry_t from configuration");
			json_object_put(entry_json);
			json_object_put(entry_metadata);
			return;
		}

		// Parse entry header using ablist_entry_t structure
		header_obj = json_object_new_object();
		if (sldm_telemetry_structure_parse(tl, ablist_entry_def,
			chunk_offset * 8 + offset * 8, header_obj, NULL) != 0) {
			SOLIDIGM_LOG_WARNING(
				"Failed to parse ablist_entry_t at offset %u", offset);
			json_object_put(header_obj);
			json_object_put(entry_json);
			json_object_put(entry_metadata);

			// Skip to next entry if we know where it is, otherwise break
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		// Parse entry data using tracker_entry_t structure
		data_obj = json_object_new_object();
		if (sldm_telemetry_structure_parse(tl, tracker_entry_def,
			chunk_offset * 8 + (offset + ablist_entry_size) * 8,
			data_obj, entry_metadata) != 0) {
			SOLIDIGM_LOG_WARNING(
				"Failed to parse tracker_entry_t at offset %u",
				offset + ablist_entry_size);
			json_object_put(header_obj);
			json_object_put(data_obj);
			json_object_put(entry_json);
			json_object_put(entry_metadata);

			// Skip to next entry if we know where it is, otherwise break
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		// Get the inner object from header_obj
		if (!json_object_object_get_ex(header_obj,
				"ablist_entry_t", &ablist_entry_obj)) {
			SOLIDIGM_LOG_WARNING(
				"Failed to find ablist_entry_t in parsed header data");
			json_object_put(header_obj);
			json_object_put(data_obj);
			json_object_put(entry_json);
			json_object_put(entry_metadata);

			// Skip to next entry if we know where it is, otherwise break
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		// Get next_entry_index from ablist_entry_obj
		if (!json_object_object_get_ex(ablist_entry_obj,
				"next_entry_index", &next_obj)) {
			SOLIDIGM_LOG_WARNING(
				"Failed to find 'next_entry_index' in ablist_entry_t");
			// Continue with next_entry_index = 0 (will likely break)
		} else {
			next_entry_index = json_object_get_int(next_obj);
		}

		// Get the inner object from data_obj - only supporting nested
		// structure format
		if (!json_object_object_get_ex(data_obj,
				"tracker_entry_t", &tracker_entry_obj)) {
			SOLIDIGM_LOG_WARNING(
				"Failed to find tracker_entry_t in parsed data");
			json_object_put(header_obj);
			json_object_put(data_obj);
			json_object_put(entry_json);
			json_object_put(entry_metadata);

			// Skip to next entry if we know where it is, otherwise break
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		// Get hash value from tracker_entry_obj
		if (!json_object_object_get_ex(tracker_entry_obj,
				"hash", &hash_obj)) {
			SOLIDIGM_LOG_WARNING(
				"Failed to find 'hash' in tracker_entry_t");
			json_object_put(header_obj);
			json_object_put(data_obj);
			json_object_put(entry_json);
			json_object_put(entry_metadata);

			// Skip to next entry if we know where it is, otherwise break
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		hash = json_object_get_int(hash_obj);

		// Get tracker entry info from config
		struct json_object *entry_info = get_tracker_entry_info(config, hash);

		// Handle case where no entry info is found
		if (!entry_info) {
			// Clean up the entry JSON since we won't be adding it to the array
			json_object_put(entry_json);

			// Skip to next entry
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		// Get tracker entry info from config
		if (json_object_object_get_ex(entry_info, "idName", &name_obj)) {
			json_object_object_add(entry_json, "name", name_obj);
			json_object_get(name_obj); // Increase ref count
		}
		if (json_object_object_get_ex(entry_info, "file", &file_obj)) {
			json_object_object_add(entry_json, "file", file_obj);
			json_object_get(file_obj); // Increase ref count
		}
		if (json_object_object_get_ex(entry_info, "line", &line_obj)) {
			json_object_object_add(entry_json, "line", line_obj);
			json_object_get(line_obj); // Increase ref count
		}

		// We need to get tracker_entry_obj again for fields
		if (!json_object_object_get_ex(data_obj,
				"tracker_entry_t", &tracker_entry_obj)) {
			// This shouldn't happen as we already checked, but let's be safe
			json_object_put(entry_json);
			json_object_put(header_obj);
			json_object_put(data_obj);
			json_object_put(entry_metadata);

			// Skip to next entry
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		if (json_object_object_get_ex(tracker_entry_obj, "time",
					   &time_obj)) {
			json_object_add_value_uint64(entry_json, "time",
				json_object_get_uint64(time_obj));
		}

		if (json_object_object_get_ex(tracker_entry_obj, "arm_id",
					   &arm_id_obj)) {
			json_object_add_value_uint64(entry_json, "arm_id",
				json_object_get_int(arm_id_obj));
		}

		// Re-get the hash obj to add the tracker_id to the JSON
		if (json_object_object_get_ex(tracker_entry_obj, "hash",
					   &hash_obj)) {
			json_object_add_value_uint64(entry_json, "hash",
				json_object_get_int(hash_obj));
		}

		if (json_object_object_get_ex(tracker_entry_obj, "level",
					   &level_obj)) {
			int level = json_object_get_int(level_obj);

			json_object_add_value_uint64(entry_json, "level", level);
			json_object_object_add(entry_json, "level_name",
						   json_object_new_string(get_level_string(level)));

		}

		if (json_object_object_get_ex(tracker_entry_obj, "group",
					   &group_obj)) {
			json_object_add_value_uint64(entry_json, "group",
				json_object_get_int(group_obj));
		}

		if (json_object_object_get_ex(tracker_entry_obj, "arg_count",
					   &arg_count_obj)) {
			arg_count = json_object_get_int(arg_count_obj);
			json_object_add_value_uint(entry_json, "arg_count", arg_count);
		}

		// Add arguments from dynamically parsed data
		arg_obj = json_object_new_object();

		// Get args array
		if (!json_object_object_get_ex(tracker_entry_obj,
				"args", &args_array) || !args_array) {
			// No args or couldn't find args array, add empty arguments object
			json_object_object_add(entry_json, "arguments", arg_obj);
			json_object_array_add(entries_array, entry_json);

			// Clean up allocated objects
			json_object_put(header_obj);
			json_object_put(data_obj);
			json_object_put(entry_metadata);

			// Skip to next entry
			if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
				break;

			offset = next_entry_index;
			entry_count--;
			continue;
		}

		// Process array items if args_array is valid
		int array_len = json_object_array_length(args_array);

		for (uint32_t i = 0; i < arg_count && i < MAX_ARGS
		     && i < (uint32_t)array_len; i++) {
			char arg_key[16] = {0};
			const char *arg_desc = entry_info ?
				get_arg_description(entry_info, i)
				: NULL;
			struct json_object *arg_val =
				json_object_array_get_idx(args_array, i);

			if (!arg_desc) {
				snprintf(arg_key, sizeof(arg_key),
					 "arg%d", i);
				arg_desc = arg_key;
			}

			if (arg_val) {
				uint32_t arg_value =
					json_object_get_int(arg_val);

				json_object_add_value_uint64(arg_obj,
					arg_desc, arg_value);
			}
		}

		// Add arguments and entry to output
		json_object_object_add(entry_json, "arguments", arg_obj);
		json_object_array_add(entries_array, entry_json);
		// Clean up any dynamically allocated objects
		json_object_put(header_obj);
		json_object_put(data_obj);
		json_object_put(entry_metadata);

		// Break if we can't continue
		if (next_entry_index == 0 || next_entry_index >= TRACKER_CHUNK_SIZE)
			break;

		// Move to next entry
		offset = next_entry_index;
		entry_count--;
	}
}

void sldm_tracker_parse(struct telemetry_log *tl, uint32_t offset, uint32_t size,
	struct json_object *tracker_obj)
{
	uint32_t chunks = size / TRACKER_CHUNK_SIZE;
	struct json_object *entries_array = json_object_new_array();

	json_object_add_value_uint(tracker_obj, "offset", offset);
	json_object_add_value_uint(tracker_obj, "size", size);
	json_object_add_value_uint(tracker_obj, "chunks", chunks);

	for (uint32_t i = 0; i < chunks; i++) {
		parse_tracker_chunk_json(tl, offset + (i * TRACKER_CHUNK_SIZE),
			i, entries_array);
	}

	json_object_object_add(tracker_obj, "entries", entries_array);
}