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
|
/*
* Copyright (C) 2019 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "i915_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 = I915_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;
}
|