-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathprofilingTest.c
149 lines (124 loc) · 4.35 KB
/
profilingTest.c
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
//
// Copyright (C) 2023 Intel Corporation.
// SPDX-License-Identifier: Apache 2.0
//
#include <stdio.h>
#include <stdlib.h>
#include "npu_driver_compiler.h"
int readFile(const char* fileName, char** buffer, size_t* size) {
FILE* file = fopen(fileName, "rb");
if (!file) {
perror("Can't open blob file");
return EXIT_FAILURE;
}
fseek(file, 0L, SEEK_END);
long fileSize = ftell(file);
if (fileSize < 0) {
printf("Ftell method returns failure.");
fclose(file);
return VCL_RESULT_ERROR_IO;
}
uint64_t unsignedFileSize = (uint64_t)fileSize;
fseek(file, 0L, SEEK_SET);
char* binaryBuffer = (char*)malloc(unsignedFileSize);
if (!binaryBuffer) {
fprintf(stderr, "Can't allocate %zu bytes to read %s.", unsignedFileSize, fileName);
fclose(file);
return EXIT_FAILURE;
}
// Use fgetc to read from the file into the buffer
int ch;
size_t bytesRead = 0;
while ((ch = fgetc(file)) != EOF && bytesRead < unsignedFileSize - 1) {
if (ch >= 32 && ch <= 126) {
binaryBuffer[bytesRead++] = (char)ch;
} else {
free(binaryBuffer);
fprintf(stderr, "Can't character is a printable ASCII character.");
fclose(file);
return EXIT_FAILURE;
}
}
// Null-terminate the buffer
buffer[bytesRead] = '\0';
if (bytesRead <= 0) {
free(binaryBuffer);
fprintf(stderr, "Binary buffer is empty");
fclose(file);
return EXIT_FAILURE;
}
fclose(file);
*buffer = binaryBuffer;
*size = unsignedFileSize;
return EXIT_SUCCESS;
}
int main(int argc, char** argv) {
if (argc != 3) {
printf("usage:\n"
"\tprofilingTest network.blob profiling_output.bin\n"
"where\n"
"\tnetwork.blob - blob with profiling enabled ('PERF_COUNT YES' parameter in the compiler)"
"\tprofiling_output.bin - raw profiling output acquired from InferenceManagerDemo according to "
"guides/how-to-use-profiling.md\n");
return EXIT_FAILURE;
}
const char* blobFileName = argv[1];
const char* profFileName = argv[2];
char* blobBuffer = NULL;
size_t blobSize = 0;
int result = readFile(blobFileName, &blobBuffer, &blobSize);
if (result != EXIT_SUCCESS) {
return result;
}
char* profBuffer = NULL;
size_t profSize = 0;
result = readFile(profFileName, &profBuffer, &profSize);
if (result != EXIT_SUCCESS) {
free(blobBuffer);
return result;
}
vcl_result_t ret = VCL_RESULT_SUCCESS;
vcl_profiling_input_t profilingApiInput = {.blobData = blobBuffer,
.blobSize = blobSize,
.profData = profBuffer,
.profSize = profSize};
vcl_profiling_handle_t profHandle = NULL;
ret = vclProfilingCreate(&profilingApiInput, &profHandle, NULL);
if (ret != VCL_RESULT_SUCCESS) {
result = EXIT_FAILURE;
goto exit;
}
vcl_profiling_properties_t profProperties;
ret = vclProfilingGetProperties(profHandle, &profProperties);
if (ret != VCL_RESULT_SUCCESS) {
result = EXIT_FAILURE;
goto exit_destroy;
}
printf("Using profiling version %hu.%hu\n", profProperties.version.major, profProperties.version.minor);
vcl_profiling_output_t profOutput;
profOutput.data = NULL;
ret = vclGetDecodedProfilingBuffer(profHandle, VCL_PROFILING_LAYER_LEVEL, &profOutput);
if (ret != VCL_RESULT_SUCCESS || profOutput.data == NULL) {
result = EXIT_FAILURE;
goto exit_destroy;
}
profOutput.data = NULL;
ret = vclGetDecodedProfilingBuffer(profHandle, VCL_PROFILING_TASK_LEVEL, &profOutput);
if (ret != VCL_RESULT_SUCCESS || profOutput.data == NULL) {
result = EXIT_FAILURE;
goto exit_destroy;
}
profOutput.data = NULL;
ret = vclGetDecodedProfilingBuffer(profHandle, VCL_PROFILING_RAW, &profOutput);
if (ret != VCL_RESULT_SUCCESS || profOutput.data == NULL) {
result = EXIT_FAILURE;
goto exit_destroy;
}
printf("Test passed. Profiling API works! Great success!\n");
exit_destroy:
vclProfilingDestroy(profHandle);
exit:
free(blobBuffer);
free(profBuffer);
return result;
}