File: xe_perf_control.c

package info (click to toggle)
intel-gpu-tools 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 63,360 kB
  • sloc: xml: 781,458; ansic: 360,567; python: 8,336; yacc: 2,781; perl: 1,196; sh: 1,177; lex: 487; asm: 227; lisp: 35; makefile: 30
file content (117 lines) | stat: -rw-r--r-- 2,792 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2024 Intel Corporation
 */


#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "xe_perf_recorder_commands.h"

static void
usage(const char *name)
{
	fprintf(stdout,
		"Usage: %s [options]\n"
		"\n"
		"     --help,               -h         Print this screen\n"
		"     --command-fifo,       -f <path>  Path to a command fifo\n"
		"     --dump,               -d <path>  Write a content of circular buffer to path\n",
		name);
}

int
main(int argc, char *argv[])
{
	const struct option long_options[] = {
		{"help",                       no_argument, 0, 'h'},
		{"dump",                 required_argument, 0, 'd'},
		{"command-fifo",         required_argument, 0, 'f'},
		{"quit",                       no_argument, 0, 'q'},
		{0, 0, 0, 0}
	};
	const char *command_fifo = XE_PERF_RECORD_FIFO_PATH, *dump_file = NULL;
	FILE *command_fifo_file;
	int opt;
	bool quit = false;

	while ((opt = getopt_long(argc, argv, "hd:f:q", long_options, NULL)) != -1) {
		switch (opt) {
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
		case 'd':
			dump_file = optarg;
			break;
		case 'f':
			command_fifo = optarg;
			break;
		case 'q':
			quit = true;
			break;
		default:
			fprintf(stderr, "Internal error: "
				"unexpected getopt value: %d\n", opt);
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}

	if (!command_fifo)
		return EXIT_FAILURE;

	command_fifo_file = fopen(command_fifo, "r+");
	if (!command_fifo_file) {
		fprintf(stderr, "Unable to open command file\n");
		return EXIT_FAILURE;
	}

	if (dump_file) {
		if (dump_file[0] == '/') {
			uint32_t total_len =
				sizeof(struct recorder_command_base) + strlen(dump_file) + 1;
			struct {
				struct recorder_command_base base;
				uint8_t dump[];
			} *data = malloc(total_len);

			data->base.command = RECORDER_COMMAND_DUMP;
			data->base.size = total_len;
			snprintf((char *) data->dump, strlen(dump_file) + 1, "%s", dump_file);

			fwrite(data, total_len, 1, command_fifo_file);
		} else {
			char *cwd = getcwd(NULL, 0);
			uint32_t path_len = strlen(cwd) + 1 + strlen(dump_file) + 1;
			uint32_t total_len = sizeof(struct recorder_command_base) + path_len;
			struct {
				struct recorder_command_base base;
				uint8_t dump[];
			} *data = malloc(total_len);

			data->base.command = RECORDER_COMMAND_DUMP;
			data->base.size = total_len;
			snprintf((char *) data->dump, path_len, "%s/%s", cwd, dump_file);

			fwrite(data, total_len, 1, command_fifo_file);
		}
	}

	if (quit) {
		struct recorder_command_base base = {
			.command = RECORDER_COMMAND_QUIT,
			.size = sizeof(base),
		};

		fwrite(&base, sizeof(base), 1, command_fifo_file);
	}

	fclose(command_fifo_file);

	return EXIT_SUCCESS;
}