This source file includes following definitions.
- rw_thread_info_new
- rw_thread_init
- bind_cpu
- rw_thread_main
- rw_thread_run
1
2
3
4
5
6
7
8
9
10 #define _GNU_SOURCE
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/syscall.h>
16 #include "trace-agent.h"
17
18 #define READ_WAIT_USEC 100000
19
20 void *rw_thread_info_new(void)
21 {
22 struct rw_thread_info *rw_ti;
23
24 rw_ti = zalloc(sizeof(struct rw_thread_info));
25 if (rw_ti == NULL) {
26 pr_err("rw_thread_info zalloc error\n");
27 exit(EXIT_FAILURE);
28 }
29
30 rw_ti->cpu_num = -1;
31 rw_ti->in_fd = -1;
32 rw_ti->out_fd = -1;
33 rw_ti->read_pipe = -1;
34 rw_ti->write_pipe = -1;
35 rw_ti->pipe_size = PIPE_INIT;
36
37 return rw_ti;
38 }
39
40 void *rw_thread_init(int cpu, const char *in_path, const char *out_path,
41 bool stdout_flag, unsigned long pipe_size,
42 struct rw_thread_info *rw_ti)
43 {
44 int data_pipe[2];
45
46 rw_ti->cpu_num = cpu;
47
48
49 rw_ti->in_fd = open(in_path, O_RDONLY);
50 if (rw_ti->in_fd == -1) {
51 pr_err("Could not open in_fd (CPU:%d)\n", cpu);
52 goto error;
53 }
54
55
56 if (!stdout_flag) {
57
58 rw_ti->out_fd = open(out_path, O_WRONLY);
59 if (rw_ti->out_fd == -1) {
60 pr_err("Could not open out_fd (CPU:%d)\n", cpu);
61 goto error;
62 }
63 } else
64
65 rw_ti->out_fd = STDOUT_FILENO;
66
67 if (pipe2(data_pipe, O_NONBLOCK) < 0) {
68 pr_err("Could not create pipe in rw-thread(%d)\n", cpu);
69 goto error;
70 }
71
72
73
74
75
76 if (fcntl(*data_pipe, F_SETPIPE_SZ, pipe_size) < 0) {
77 pr_err("Could not change pipe size in rw-thread(%d)\n", cpu);
78 goto error;
79 }
80
81 rw_ti->read_pipe = data_pipe[1];
82 rw_ti->write_pipe = data_pipe[0];
83 rw_ti->pipe_size = pipe_size;
84
85 return NULL;
86
87 error:
88 exit(EXIT_FAILURE);
89 }
90
91
92 static void bind_cpu(int cpu_num)
93 {
94 cpu_set_t mask;
95
96 CPU_ZERO(&mask);
97 CPU_SET(cpu_num, &mask);
98
99
100 if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
101 pr_err("Could not set CPU#%d affinity\n", (int)cpu_num);
102 }
103
104 static void *rw_thread_main(void *thread_info)
105 {
106 ssize_t rlen, wlen;
107 ssize_t ret;
108 struct rw_thread_info *ts = (struct rw_thread_info *)thread_info;
109
110 bind_cpu(ts->cpu_num);
111
112 while (1) {
113
114 if (!global_run_operation) {
115 pthread_mutex_lock(&mutex_notify);
116 pthread_cond_wait(&cond_wakeup, &mutex_notify);
117 pthread_mutex_unlock(&mutex_notify);
118 }
119
120 if (global_sig_receive)
121 break;
122
123
124
125
126
127 rlen = splice(ts->in_fd, NULL, ts->read_pipe, NULL,
128 ts->pipe_size, SPLICE_F_MOVE | SPLICE_F_MORE);
129
130 if (rlen < 0) {
131 pr_err("Splice_read in rw-thread(%d)\n", ts->cpu_num);
132 goto error;
133 } else if (rlen == 0) {
134
135
136
137
138
139
140 usleep(READ_WAIT_USEC);
141 pr_debug("Read retry(cpu:%d)\n", ts->cpu_num);
142 continue;
143 }
144
145 wlen = 0;
146
147 do {
148 ret = splice(ts->write_pipe, NULL, ts->out_fd, NULL,
149 rlen - wlen,
150 SPLICE_F_MOVE | SPLICE_F_MORE);
151
152 if (ret < 0) {
153 pr_err("Splice_write in rw-thread(%d)\n",
154 ts->cpu_num);
155 goto error;
156 } else if (ret == 0)
157
158
159
160
161
162
163
164
165
166 sleep(1);
167 wlen += ret;
168 } while (wlen < rlen);
169 }
170
171 return NULL;
172
173 error:
174 exit(EXIT_FAILURE);
175 }
176
177
178 pthread_t rw_thread_run(struct rw_thread_info *rw_ti)
179 {
180 int ret;
181 pthread_t rw_thread_per_cpu;
182
183 ret = pthread_create(&rw_thread_per_cpu, NULL, rw_thread_main, rw_ti);
184 if (ret != 0) {
185 pr_err("Could not create a rw thread(%d)\n", rw_ti->cpu_num);
186 exit(EXIT_FAILURE);
187 }
188
189 return rw_thread_per_cpu;
190 }