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