]>
Commit | Line | Data |
---|---|---|
148405fb JH |
1 | /* |
2 | * test-fsmonitor-client.c: client code to send commands/requests to | |
3 | * a `git fsmonitor--daemon` daemon. | |
4 | */ | |
5 | ||
6 | #include "test-tool.h" | |
7 | #include "cache.h" | |
8 | #include "parse-options.h" | |
9 | #include "fsmonitor-ipc.h" | |
e38da487 | 10 | #include "setup.h" |
49b398a9 JH |
11 | #include "thread-utils.h" |
12 | #include "trace2.h" | |
d5ebb50d | 13 | #include "wrapper.h" |
148405fb JH |
14 | |
15 | #ifndef HAVE_FSMONITOR_DAEMON_BACKEND | |
16 | int cmd__fsmonitor_client(int argc, const char **argv) | |
17 | { | |
18 | die("fsmonitor--daemon not available on this platform"); | |
19 | } | |
20 | #else | |
21 | ||
22 | /* | |
23 | * Read the `.git/index` to get the last token written to the | |
24 | * FSMonitor Index Extension. | |
25 | */ | |
26 | static const char *get_token_from_index(void) | |
27 | { | |
28 | struct index_state *istate = the_repository->index; | |
29 | ||
30 | if (do_read_index(istate, the_repository->index_file, 0) < 0) | |
31 | die("unable to read index file"); | |
32 | if (!istate->fsmonitor_last_update) | |
33 | die("index file does not have fsmonitor extension"); | |
34 | ||
35 | return istate->fsmonitor_last_update; | |
36 | } | |
37 | ||
38 | /* | |
39 | * Send an IPC query to a `git-fsmonitor--daemon` daemon and | |
40 | * ask for the changes since the given token or from the last | |
41 | * token in the index extension. | |
42 | * | |
43 | * This will implicitly start a daemon process if necessary. The | |
44 | * daemon process will persist after we exit. | |
45 | */ | |
46 | static int do_send_query(const char *token) | |
47 | { | |
48 | struct strbuf answer = STRBUF_INIT; | |
49 | int ret; | |
50 | ||
51 | if (!token || !*token) | |
52 | token = get_token_from_index(); | |
53 | ||
54 | ret = fsmonitor_ipc__send_query(token, &answer); | |
55 | if (ret < 0) | |
56 | die("could not query fsmonitor--daemon"); | |
57 | ||
58 | write_in_full(1, answer.buf, answer.len); | |
59 | strbuf_release(&answer); | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
64 | /* | |
65 | * Send a "flush" command to the `git-fsmonitor--daemon` (if running) | |
66 | * and tell it to flush its cache. | |
67 | * | |
68 | * This feature is primarily used by the test suite to simulate a loss of | |
69 | * sync with the filesystem where we miss kernel events. | |
70 | */ | |
71 | static int do_send_flush(void) | |
72 | { | |
73 | struct strbuf answer = STRBUF_INIT; | |
74 | int ret; | |
75 | ||
76 | ret = fsmonitor_ipc__send_command("flush", &answer); | |
77 | if (ret) | |
78 | return ret; | |
79 | ||
80 | write_in_full(1, answer.buf, answer.len); | |
81 | strbuf_release(&answer); | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
49b398a9 JH |
86 | struct hammer_thread_data |
87 | { | |
88 | pthread_t pthread_id; | |
89 | int thread_nr; | |
90 | ||
91 | int nr_requests; | |
92 | const char *token; | |
93 | ||
94 | int sum_successful; | |
95 | int sum_errors; | |
96 | }; | |
97 | ||
98 | static void *hammer_thread_proc(void *_hammer_thread_data) | |
99 | { | |
100 | struct hammer_thread_data *data = _hammer_thread_data; | |
101 | struct strbuf answer = STRBUF_INIT; | |
102 | int k; | |
103 | int ret; | |
104 | ||
105 | trace2_thread_start("hammer"); | |
106 | ||
107 | for (k = 0; k < data->nr_requests; k++) { | |
108 | strbuf_reset(&answer); | |
109 | ||
110 | ret = fsmonitor_ipc__send_query(data->token, &answer); | |
111 | if (ret < 0) | |
112 | data->sum_errors++; | |
113 | else | |
114 | data->sum_successful++; | |
115 | } | |
116 | ||
117 | strbuf_release(&answer); | |
118 | trace2_thread_exit(); | |
119 | return NULL; | |
120 | } | |
121 | ||
122 | /* | |
123 | * Start a pool of client threads that will each send a series of | |
124 | * commands to the daemon. | |
125 | * | |
126 | * The goal is to overload the daemon with a sustained series of | |
127 | * concurrent requests. | |
128 | */ | |
129 | static int do_hammer(const char *token, int nr_threads, int nr_requests) | |
130 | { | |
131 | struct hammer_thread_data *data = NULL; | |
132 | int k; | |
133 | int sum_join_errors = 0; | |
134 | int sum_commands = 0; | |
135 | int sum_errors = 0; | |
136 | ||
137 | if (!token || !*token) | |
138 | token = get_token_from_index(); | |
139 | if (nr_threads < 1) | |
140 | nr_threads = 1; | |
141 | if (nr_requests < 1) | |
142 | nr_requests = 1; | |
143 | ||
144 | CALLOC_ARRAY(data, nr_threads); | |
145 | ||
146 | for (k = 0; k < nr_threads; k++) { | |
147 | struct hammer_thread_data *p = &data[k]; | |
148 | p->thread_nr = k; | |
149 | p->nr_requests = nr_requests; | |
150 | p->token = token; | |
151 | ||
152 | if (pthread_create(&p->pthread_id, NULL, hammer_thread_proc, p)) { | |
153 | warning("failed to create thread[%d] skipping remainder", k); | |
154 | nr_threads = k; | |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
159 | for (k = 0; k < nr_threads; k++) { | |
160 | struct hammer_thread_data *p = &data[k]; | |
161 | ||
162 | if (pthread_join(p->pthread_id, NULL)) | |
163 | sum_join_errors++; | |
164 | sum_commands += p->sum_successful; | |
165 | sum_errors += p->sum_errors; | |
166 | } | |
167 | ||
168 | fprintf(stderr, "HAMMER: [threads %d][requests %d] [ok %d][err %d][join %d]\n", | |
169 | nr_threads, nr_requests, sum_commands, sum_errors, sum_join_errors); | |
170 | ||
171 | free(data); | |
172 | ||
173 | /* | |
174 | * Return an error if any of the _send_query requests failed. | |
175 | * We don't care about thread create/join errors. | |
176 | */ | |
177 | return sum_errors > 0; | |
178 | } | |
179 | ||
148405fb JH |
180 | int cmd__fsmonitor_client(int argc, const char **argv) |
181 | { | |
182 | const char *subcmd; | |
183 | const char *token = NULL; | |
49b398a9 JH |
184 | int nr_threads = 1; |
185 | int nr_requests = 1; | |
148405fb JH |
186 | |
187 | const char * const fsmonitor_client_usage[] = { | |
188 | "test-tool fsmonitor-client query [<token>]", | |
189 | "test-tool fsmonitor-client flush", | |
49b398a9 | 190 | "test-tool fsmonitor-client hammer [<token>] [<threads>] [<requests>]", |
148405fb JH |
191 | NULL, |
192 | }; | |
193 | ||
194 | struct option options[] = { | |
195 | OPT_STRING(0, "token", &token, "token", | |
196 | "command token to send to the server"), | |
49b398a9 JH |
197 | |
198 | OPT_INTEGER(0, "threads", &nr_threads, "number of client threads"), | |
199 | OPT_INTEGER(0, "requests", &nr_requests, "number of requests per thread"), | |
200 | ||
148405fb JH |
201 | OPT_END() |
202 | }; | |
203 | ||
204 | argc = parse_options(argc, argv, NULL, options, fsmonitor_client_usage, 0); | |
205 | ||
206 | if (argc != 1) | |
207 | usage_with_options(fsmonitor_client_usage, options); | |
208 | ||
209 | subcmd = argv[0]; | |
210 | ||
211 | setup_git_directory(); | |
212 | ||
213 | if (!strcmp(subcmd, "query")) | |
214 | return !!do_send_query(token); | |
215 | ||
216 | if (!strcmp(subcmd, "flush")) | |
217 | return !!do_send_flush(); | |
218 | ||
49b398a9 JH |
219 | if (!strcmp(subcmd, "hammer")) |
220 | return !!do_hammer(token, nr_threads, nr_requests); | |
221 | ||
148405fb JH |
222 | die("Unhandled subcommand: '%s'", subcmd); |
223 | } | |
224 | #endif |