-
Notifications
You must be signed in to change notification settings - Fork 2
/
tee-stats.c
207 lines (177 loc) · 5.49 KB
/
tee-stats.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
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
/*
* Copyright (c) 2016, Linaro Limited
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <tee_client_api.h>
static int verbosity;
static int clear;
#define _verbose(lvl, ...) \
do { \
if (verbosity >= lvl) { \
printf(__VA_ARGS__); \
fflush(stdout); \
} \
} while (0)
#define verbose(...) _verbose(1, __VA_ARGS__)
#define vverbose(...) _verbose(2, __VA_ARGS__)
/*
* TA interface. Keep in sync with OP-TEE OS!
* - core/include/mm/tee_mm.h
* - core/arch/arm/sta/stats.c
*/
#define STATS_UUID { 0xd96a5b40, 0xe2c7, 0xb1af, \
{ 0x87, 0x94, 0x10, 0x02, 0xa5, 0xd5, 0xc6, 0x1b } }
#define TEE_MM_POOL_DESC_LENGTH 32
struct tee_mm_pool_stats {
char desc[TEE_MM_POOL_DESC_LENGTH];
uint32_t allocated;
uint32_t max_allocated;
uint32_t size;
uint32_t num_alloc_fail;
uint32_t biggest_alloc_fail;
uint32_t biggest_alloc_fail_used;
};
#define STATS_CMD_ALLOC_STATS 1
/* End of TA interface */
static void errx(const char *msg, TEEC_Result res)
{
fprintf(stderr, "%s: 0x%08x\n", msg, res);
exit (1);
}
static void check_res(TEEC_Result res, const char *errmsg)
{
if (res != TEEC_SUCCESS)
errx(errmsg, res);
}
#define _TO_STR(x) #x
#define TO_STR(x) _TO_STR(x)
static void usage(const char *progname)
{
fprintf(stderr, "Statistics gathering tool for OP-TEE (%s)\n\n",
TO_STR(VERSION));
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s -h\n", progname);
fprintf(stderr, " %s [-v] [-c] [<what>]\n\n", progname);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -c Clear counters after getting them\n");
fprintf(stderr, " -h Print this help and exit\n");
fprintf(stderr, " -v Be verbose (use twice for greater effect)\n\n");
fprintf(stderr, "Values for <what>:\n");
fprintf(stderr, " heap The main TEE core heap (default)\n");
fprintf(stderr, " taheap The secure heap used to map user-mode TAs\n");
}
static const char *heap_to_str(uint32_t id)
{
switch (id) {
case 1:
return "HEAP";
case 3:
return "TAHEAP";
default:
return "???";
}
}
static void get_stats(uint32_t heap_id)
{
TEEC_Result res;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_Operation op;
uint32_t ret_origin;
TEEC_UUID uuid = STATS_UUID;
struct tee_mm_pool_stats st;
res = TEEC_InitializeContext(NULL, &ctx);
check_res(res,"TEEC_InitializeContext");
res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
NULL, &ret_origin);
if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
fprintf(stderr, "TEEC_OpenSession: TEEC_ERROR_ITEM_NOT_FOUND "
"(did you forget to build OP-TEE with "
"CFG_WITH_STATS=y?)\n");
exit(1);
}
check_res(res,"TEEC_OpenSession");
memset(&op, 0, sizeof(op));
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_OUTPUT,
TEEC_NONE, TEEC_NONE);
op.params[0].value.a = heap_id;
op.params[0].value.b = clear;
op.params[1].tmpref.buffer = (void *)&st;
op.params[1].tmpref.size = sizeof(st);
res = TEEC_InvokeCommand(&sess, STATS_CMD_ALLOC_STATS, &op,
&ret_origin);
check_res(res, "TEEC_InvokeCommand");
printf("%s: heap_size %u cur_alloc %u max_alloc %u fail %u "
"biggest_fail_size %u biggest_fail_used %u\n",
heap_to_str(heap_id),
st.size, st.allocated, st.max_allocated, st.num_alloc_fail,
st.biggest_alloc_fail, st.biggest_alloc_fail_used);
TEEC_CloseSession(&sess);
}
#define NEXT_ARG(i) \
do { \
if (++i == argc) { \
fprintf(stderr, "%s: %s: missing argument\n", \
argv[0], argv[i-1]); \
return 1; \
} \
} while (0);
int main(int argc, char *argv[])
{
int i;
struct timespec ts;
uint32_t heap_id = 1;
/* Parse command line */
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-h")) {
usage(argv[0]);
return 0;
}
}
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
clear = 1;
} else if (!strcmp(argv[i], "-v")) {
verbosity++;
} else if (!strcmp(argv[i], "heap")) {
/* This is the default command */
} else if (!strcmp(argv[i], "taheap")) {
heap_id = 3;
} else {
fprintf(stderr, "%s: invalid argument: %s\n",
argv[0], argv[i]);
usage(argv[0]);
return 1;
}
}
vverbose("tee-stats version %s\n", TO_STR(VERSION));
get_stats(heap_id);
return 0;
}