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