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
|
/* -*- linux-c -*-
*
* relay_old.c - staprun relayfs functions for kernels with
* old relayfs implementations.
*
* This file is part of systemtap, and is free software. You can
* redistribute it and/or modify it under the terms of the GNU General
* Public License (GPL); either version 2, or (at your option) any
* later version.
*
* Copyright (C) 2005-2007 Red Hat Inc.
*/
#include "staprun.h"
/* temporary per-cpu output written here for relayfs, filebase0...N */
static int relay_fd[NR_CPUS];
static int proc_fd[NR_CPUS];
static FILE *percpu_tmpfile[NR_CPUS];
static char *relay_buffer[NR_CPUS];
static pthread_t reader[NR_CPUS];
static int bulkmode = 0;
unsigned subbuf_size = 0;
unsigned n_subbufs = 0;
/* per-cpu buffer info */
static struct buf_status
{
struct _stp_buf_info info;
unsigned max_backlog; /* max # sub-buffers ready at one time */
} status[NR_CPUS];
/**
* close_relayfs_files - close and munmap buffer and open output file
*/
static void close_relayfs_files(int cpu)
{
size_t total_bufsize = subbuf_size * n_subbufs;
if (relay_fd[cpu]) {
munmap(relay_buffer[cpu], total_bufsize);
close(relay_fd[cpu]);
close(proc_fd[cpu]);
relay_fd[cpu] = 0;
fclose(percpu_tmpfile[cpu]);
}
}
/**
* close_all_relayfs_files - close and munmap buffers and output files
*/
void close_oldrelayfs(int detach)
{
int i;
if (!bulkmode)
return;
dbug(2, "detach=%d, ncpus=%d\n", detach, ncpus);
if (detach) {
for (i = 0; i < ncpus; i++)
if (reader[i]) pthread_cancel(reader[i]);
} else {
for (i = 0; i < ncpus; i++)
if (reader[i]) pthread_join(reader[i], NULL);
}
for (i = 0; i < ncpus; i++)
close_relayfs_files(i);
}
/**
* open_relayfs_files - open and mmap buffer and open output file.
* Returns -1 on unexpected failure, 0 if file not found, 1 on success.
*/
static int open_relayfs_files(int cpu, const char *relay_filebase, const char *proc_filebase)
{
size_t total_bufsize;
char tmp[PATH_MAX];
memset(&status[cpu], 0, sizeof(struct buf_status));
status[cpu].info.cpu = cpu;
sprintf(tmp, "%s%d", relay_filebase, cpu);
relay_fd[cpu] = open(tmp, O_RDONLY | O_NONBLOCK);
if (relay_fd[cpu] < 0) {
relay_fd[cpu] = 0;
return 0;
}
sprintf(tmp, "%s%d", proc_filebase, cpu);
dbug(2, "Opening %s.\n", tmp);
proc_fd[cpu] = open(tmp, O_RDWR | O_NONBLOCK);
if (proc_fd[cpu] < 0) {
fprintf(stderr, "ERROR: couldn't open proc file %s: errcode = %s\n", tmp, strerror(errno));
goto err1;
}
if (outfile_name) {
/* special case: for testing we sometimes want to write to /dev/null */
if (strcmp(outfile_name, "/dev/null") == 0)
strcpy(tmp, outfile_name);
else
sprintf(tmp, "%s_%d", outfile_name, cpu);
} else
sprintf(tmp, "stpd_cpu%d", cpu);
if((percpu_tmpfile[cpu] = fopen(tmp, "w+")) == NULL) {
fprintf(stderr, "ERROR: Couldn't open output file %s: errcode = %s\n", tmp, strerror(errno));
goto err2;
}
total_bufsize = subbuf_size * n_subbufs;
relay_buffer[cpu] = mmap(NULL, total_bufsize, PROT_READ,
MAP_PRIVATE | MAP_POPULATE, relay_fd[cpu],
0);
if(relay_buffer[cpu] == MAP_FAILED)
{
fprintf(stderr, "ERROR: couldn't mmap relay file, total_bufsize (%d) = subbuf_size (%d) * n_subbufs(%d), error = %s \n", (int)total_bufsize, (int)subbuf_size, (int)n_subbufs, strerror(errno));
goto err3;
}
return 1;
err3:
fclose(percpu_tmpfile[cpu]);
err2:
close (proc_fd[cpu]);
err1:
close (relay_fd[cpu]);
relay_fd[cpu] = 0;
return -1;
}
/**
* process_subbufs - write ready subbufs to disk
*/
static int process_subbufs(struct _stp_buf_info *info)
{
unsigned subbufs_ready, start_subbuf, end_subbuf, subbuf_idx, i;
int len, cpu = info->cpu;
char *subbuf_ptr;
int subbufs_consumed = 0;
unsigned padding;
subbufs_ready = info->produced - info->consumed;
start_subbuf = info->consumed % n_subbufs;
end_subbuf = start_subbuf + subbufs_ready;
for (i = start_subbuf; i < end_subbuf; i++) {
subbuf_idx = i % n_subbufs;
subbuf_ptr = relay_buffer[cpu] + subbuf_idx * subbuf_size;
padding = *((unsigned *)subbuf_ptr);
subbuf_ptr += sizeof(padding);
len = (subbuf_size - sizeof(padding)) - padding;
if (len) {
if (fwrite_unlocked (subbuf_ptr, len, 1, percpu_tmpfile[cpu]) != 1) {
fprintf(stderr, "ERROR: couldn't write to output file for cpu %d, exiting: errcode = %d: %s\n", cpu, errno, strerror(errno));
exit(1);
}
}
subbufs_consumed++;
}
return subbufs_consumed;
}
/**
* reader_thread - per-cpu channel buffer reader
*/
static void *reader_thread(void *data)
{
int rc;
int cpu = (long)data;
struct pollfd pollfd;
struct _stp_consumed_info consumed_info;
unsigned subbufs_consumed;
cpu_set_t cpu_mask;
CPU_ZERO(&cpu_mask);
CPU_SET(cpu, &cpu_mask);
if( sched_setaffinity( 0, sizeof(cpu_mask), &cpu_mask ) < 0 ) {
perror("sched_setaffinity");
}
pollfd.fd = relay_fd[cpu];
pollfd.events = POLLIN;
do {
rc = poll(&pollfd, 1, -1);
if (rc < 0) {
if (errno != EINTR) {
fprintf(stderr, "ERROR: poll error: %s\n",
strerror(errno));
exit(1);
}
fprintf(stderr, "WARNING: poll warning: %s\n",
strerror(errno));
rc = 0;
}
rc = read(proc_fd[cpu], &status[cpu].info, sizeof(struct _stp_buf_info));
subbufs_consumed = process_subbufs(&status[cpu].info);
if (subbufs_consumed) {
if (subbufs_consumed > status[cpu].max_backlog)
status[cpu].max_backlog = subbufs_consumed;
status[cpu].info.consumed += subbufs_consumed;
consumed_info.cpu = cpu;
consumed_info.consumed = subbufs_consumed;
if (write (proc_fd[cpu], &consumed_info, sizeof(struct _stp_consumed_info)) < 0)
fprintf(stderr,"WARNING: writing consumed info failed.\n");
}
if (status[cpu].info.flushing)
pthread_exit(NULL);
} while (1);
}
/**
* init_relayfs - create files and threads for relayfs processing
*
* Returns 0 if successful, negative otherwise
*/
int init_oldrelayfs(void)
{
int i, j;
struct statfs st;
char relay_filebase[128], proc_filebase[128];
dbug(2, "initializing relayfs.n_subbufs=%d subbuf_size=%d\n", n_subbufs, subbuf_size);
if (n_subbufs)
bulkmode = 1;
if (!bulkmode) {
if (outfile_name) {
out_fd[0] = open (outfile_name, O_CREAT|O_TRUNC|O_WRONLY, 0666);
if (out_fd[0] < 0) {
fprintf(stderr, "ERROR: couldn't open output file %s.\n", outfile_name);
return -1;
}
} else
out_fd[0] = STDOUT_FILENO;
return 0;
}
if (statfs("/sys/kernel/debug", &st) == 0 && (int) st.f_type == (int) DEBUGFS_MAGIC) {
sprintf(relay_filebase, "/sys/kernel/debug/systemtap/%s/trace", modname);
sprintf(proc_filebase, "/sys/kernel/debug/systemtap/%s/", modname);
} else if (statfs("/mnt/relay", &st) == 0 && (int) st.f_type == (int) RELAYFS_MAGIC) {
sprintf(relay_filebase, "/mnt/relay/systemtap/%s/trace", modname);
sprintf(proc_filebase, "/proc/systemtap/%s/", modname);
} else {
fprintf(stderr,"Cannot find relayfs or debugfs mount point.\n");
return -1;
}
reader[0] = (pthread_t)0;
relay_fd[0] = 0;
out_fd[0] = 0;
for (i = 0; i < NR_CPUS; i++) {
int ret = open_relayfs_files(i, relay_filebase, proc_filebase);
if (ret == 0)
break;
if (ret < 0) {
fprintf(stderr, "ERROR: couldn't open relayfs files, cpu = %d\n", i);
goto err;
}
}
ncpus = i;
dbug(2, "ncpus=%d\n", ncpus);
for (i = 0; i < ncpus; i++) {
/* create a thread for each per-cpu buffer */
if (pthread_create(&reader[i], NULL, reader_thread, (void *)(long)i) < 0) {
close_relayfs_files(i);
fprintf(stderr, "ERROR: Couldn't create reader thread, cpu = %d\n", i);
goto err;
}
}
return 0;
err:
for (j = 0; j < i; j++)
close_relayfs_files(j);
for (j = 0; j < i; j++)
if (reader[j]) pthread_cancel(reader[j]);
return -1;
}
|