[29f0dd]: daemon / opd_trans.c  Maximize  Restore  History

Download this file

338 lines (268 with data), 7.3 kB

  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
/**
* @file daemon/opd_trans.c
* Processing the sample buffer
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author John Levon
* @author Philippe Elie
* Modified by Aravind Menon for Xen
* These modifications are:
* Copyright (C) 2005 Hewlett-Packard Co.
*/
#include "opd_trans.h"
#include "opd_kernel.h"
#include "opd_sfile.h"
#include "opd_anon.h"
#include "opd_stats.h"
#include "opd_printf.h"
#include "opd_interface.h"
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
extern size_t kernel_pointer_size;
void clear_trans_last(struct transient * trans)
{
trans->last = NULL;
trans->last_anon = NULL;
}
void clear_trans_current(struct transient * trans)
{
trans->current = NULL;
trans->anon = NULL;
}
static inline void update_trans_last(struct transient * trans)
{
trans->last = trans->current;
trans->last_anon = trans->anon;
trans->last_pc = trans->pc;
}
static inline int is_escape_code(uint64_t code)
{
return kernel_pointer_size == 4 ? code == ~0LU : code == ~0LLU;
}
static uint64_t pop_buffer_value(struct transient * trans)
{
uint64_t val;
if (!trans->remaining) {
fprintf(stderr, "BUG: popping empty buffer !\n");
abort();
}
if (kernel_pointer_size == 4) {
uint32_t const * lbuf = (void const *)trans->buffer;
val = *lbuf;
} else {
uint64_t const * lbuf = (void const *)trans->buffer;
val = *lbuf;
}
trans->remaining--;
trans->buffer += kernel_pointer_size;
return val;
}
static int enough_remaining(struct transient * trans, size_t size)
{
if (trans->remaining >= size)
return 1;
verbprintf(vmisc, "Dangling ESCAPE_CODE.\n");
opd_stats[OPD_DANGLING_CODE]++;
return 0;
}
static void opd_put_sample(struct transient * trans, unsigned long long pc)
{
unsigned long long event;
if (!enough_remaining(trans, 1)) {
trans->remaining = 0;
return;
}
event = pop_buffer_value(trans);
if (trans->tracing != TRACING_ON)
trans->event = event;
trans->pc = pc;
/* sfile can change at each sample for kernel */
if (trans->in_kernel != 0)
clear_trans_current(trans);
if (!trans->in_kernel && trans->cookie == NO_COOKIE)
trans->anon = find_anon_mapping(trans);
/* get the current sfile if needed */
if (!trans->current)
trans->current = sfile_find(trans);
/*
* can happen if kernel sample falls through the cracks, or if
* it's a sample from an anon region we couldn't find
*/
if (!trans->current)
goto out;
/* FIXME: this logic is perhaps too harsh? */
if (trans->current->ignored || (trans->last && trans->last->ignored))
goto out;
/* log the sample or arc */
sfile_log_sample(trans);
out:
/* switch to trace mode */
if (trans->tracing == TRACING_START)
trans->tracing = TRACING_ON;
update_trans_last(trans);
}
static void code_unknown(struct transient * trans __attribute__((unused)))
{
fprintf(stderr, "Unknown code !\n");
abort();
}
static void code_ctx_switch(struct transient * trans)
{
clear_trans_current(trans);
if (!enough_remaining(trans, 5)) {
trans->remaining = 0;
return;
}
trans->tid = pop_buffer_value(trans);
trans->app_cookie = pop_buffer_value(trans);
/* must be ESCAPE_CODE, CTX_TGID_CODE, tgid. Like this
* because tgid was added later in a compatible manner.
*/
pop_buffer_value(trans);
pop_buffer_value(trans);
trans->tgid = pop_buffer_value(trans);
if (vmisc) {
char const * app = find_cookie(trans->app_cookie);
printf("CTX_SWITCH to tid %lu, tgid %lu, cookie %llx(%s)\n",
(unsigned long)trans->tid, (unsigned long)trans->tgid,
trans->app_cookie, app ? app : "none");
}
}
static void code_cpu_switch(struct transient * trans)
{
clear_trans_current(trans);
if (!enough_remaining(trans, 1)) {
trans->remaining = 0;
return;
}
trans->cpu = pop_buffer_value(trans);
verbprintf(vmisc, "CPU_SWITCH to %lu\n", trans->cpu);
}
static void code_cookie_switch(struct transient * trans)
{
clear_trans_current(trans);
if (!enough_remaining(trans, 1)) {
trans->remaining = 0;
return;
}
trans->cookie = pop_buffer_value(trans);
if (vmisc) {
char const * name = verbose_cookie(trans->cookie);
verbprintf(vmisc, "COOKIE_SWITCH to cookie %s(%llx)\n",
name, trans->cookie);
}
}
static void code_kernel_enter(struct transient * trans)
{
verbprintf(vmisc, "KERNEL_ENTER_SWITCH to kernel\n");
trans->in_kernel = 1;
clear_trans_current(trans);
/* subtlety: we must keep trans->cookie cached,
* even though it's meaningless for the kernel -
* we won't necessarily get a cookie switch on
* kernel exit. See comments in opd_sfile.c
*/
}
static void code_user_enter(struct transient * trans)
{
verbprintf(vmisc, "USER_ENTER_SWITCH to user-space\n");
trans->in_kernel = 0;
clear_trans_current(trans);
clear_trans_last(trans);
}
static void code_module_loaded(struct transient * trans __attribute__((unused)))
{
verbprintf(vmodule, "MODULE_LOADED_CODE\n");
opd_reread_module_info();
clear_trans_current(trans);
clear_trans_last(trans);
}
/*
* This also implicitly signals the end of the previous
* trace, so we never explicitly set TRACING_OFF when
* processing a buffer.
*/
static void code_trace_begin(struct transient * trans)
{
verbprintf(varcs, "TRACE_BEGIN\n");
trans->tracing = TRACING_START;
}
static void code_xen_enter(struct transient *trans)
{
verbprintf(vmisc, "XEN_ENTER_SWITCH to xen\n");
trans->in_kernel = 1;
trans->current = NULL;
/* subtlety: we must keep trans->cookie cached, even though it's
* meaningless for Xen - we won't necessarily get a cookie switch
* on Xen exit. See comments in opd_sfile.c. It seems that we can
* get away with in_kernel = 1 as long as we supply the correct
* Xen image, and its address range in startup find_kernel_image
* is modified to look in the Xen image also
*/
}
typedef void (*handler_t)(struct transient *);
static handler_t handlers[LAST_CODE + 1] = {
&code_unknown,
&code_ctx_switch,
&code_cpu_switch,
&code_cookie_switch,
&code_kernel_enter,
&code_user_enter,
&code_module_loaded,
/* tgid handled differently */
&code_unknown,
&code_trace_begin,
&code_unknown,
&code_xen_enter,
};
void opd_process_samples(char const * buffer, size_t count)
{
struct transient trans = {
.buffer = buffer,
.remaining = count,
.tracing = TRACING_OFF,
.current = NULL,
.last = NULL,
.cookie = INVALID_COOKIE,
.app_cookie = INVALID_COOKIE,
.anon = NULL,
.last_anon = NULL,
.pc = 0,
.last_pc = 0,
.event = 0,
.in_kernel = -1,
.cpu = -1,
.tid = -1,
.tgid = -1
};
/* FIXME: was uint64_t but it can't compile on alpha where uint64_t
* is an unsigned long and below the printf("..." %llu\n", code)
* generate a warning, this look like a stopper to use c98 types :/
*/
unsigned long long code;
while (trans.remaining) {
code = pop_buffer_value(&trans);
if (!is_escape_code(code)) {
opd_put_sample(&trans, code);
continue;
}
if (!trans.remaining) {
verbprintf(vmisc, "Dangling ESCAPE_CODE.\n");
opd_stats[OPD_DANGLING_CODE]++;
break;
}
// started with ESCAPE_CODE, next is type
code = pop_buffer_value(&trans);
if (code >= LAST_CODE) {
fprintf(stderr, "Unknown code %llu\n", code);
abort();
}
handlers[code](&trans);
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks