Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[0d0e60]: daemon / opd_trans.c Maximize Restore History

Download this file

opd_trans.c    288 lines (224 with data), 5.8 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
/**
* @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
*/
#include "opd_trans.h"
#include "opd_kernel.h"
#include "opd_sfile.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;
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;
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)
trans->current = NULL;
/* get the current sfile if needed */
if (!trans->current)
trans->current = sfile_find(trans);
/* can happen if kernel sample falls through the cracks */
if (!trans->current)
goto out;
if (trans->current->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;
/* used for callgraph only */
trans->last = trans->current;
trans->last_pc = trans->pc;
}
static void code_unknown(struct transient * trans __attribute__((unused)))
{
fprintf(stderr, "Unknown code !\n");
abort();
}
static void code_ctx_switch(struct transient * trans)
{
trans->current = NULL;
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 (verbose) {
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)
{
trans->current = NULL;
if (!enough_remaining(trans, 1)) {
trans->remaining = 0;
return;
}
trans->cpu = pop_buffer_value(trans);
verbprintf("CPU_SWITCH to %lu\n", trans->cpu);
}
static void code_cookie_switch(struct transient * trans)
{
trans->current = NULL;
if (!enough_remaining(trans, 1)) {
trans->remaining = 0;
return;
}
trans->cookie = pop_buffer_value(trans);
if (verbose) {
char const * name = verbose_cookie(trans->cookie);
verbprintf("COOKIE_SWITCH to cookie %s(%llx)\n",
name, trans->cookie);
}
}
static void code_kernel_enter(struct transient * trans)
{
verbprintf("KERNEL_ENTER_SWITCH to kernel\n");
trans->in_kernel = 1;
trans->current = NULL;
/* 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_kernel_exit(struct transient * trans)
{
verbprintf("KERNEL_EXIT_SWITCH to user-space\n");
trans->in_kernel = 0;
trans->current = NULL;
}
static void code_module_loaded(struct transient * trans __attribute__((unused)))
{
verbprintf("MODULE_LOADED_CODE\n");
opd_reread_module_info();
trans->current = NULL;
}
static void code_trace_begin(struct transient * trans)
{
verbprintf("TRACE_BEGIN\n");
trans->tracing = TRACING_START;
}
static void code_trace_end(struct transient * trans)
{
verbprintf("TRACE_END\n");
trans->tracing = TRACING_OFF;
}
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_kernel_exit,
&code_module_loaded,
&code_unknown, /* tgid handled differently */
&code_trace_begin,
&code_trace_end,
};
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 = 0,
.app_cookie = 0,
.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("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);
}
}