Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/helper/test-fsmonitor-client.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / helper / test-fsmonitor-client.c` 226 lines ยท 5236 bytes Raw Blame History 1 /* 2 * test-fsmonitor-client.c: client code to send commands/requests to 3 * a `git fsmonitor--daemon` daemon. 4 */ 5 6 #define USE_THE_REPOSITORY_VARIABLE 7 8 #include "test-tool.h" 9 #include "parse-options.h" 10 #include "fsmonitor-ipc.h" 11 #include "read-cache-ll.h" 12 #include "repository.h" 13 #include "setup.h" 14 #include "thread-utils.h" 15 #include "trace2.h" 16 17 #ifndef HAVE_FSMONITOR_DAEMON_BACKEND 18 int cmd__fsmonitor_client(int argc UNUSED, const char **argv UNUSED) 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 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 182 int cmd__fsmonitor_client(int argc, const char **argv) 183 { 184 const char *subcmd; 185 const char *token = NULL; 186 int nr_threads = 1; 187 int nr_requests = 1; 188 189 const char * const fsmonitor_client_usage[] = { 190 "test-tool fsmonitor-client query []", 191 "test-tool fsmonitor-client flush", 192 "test-tool fsmonitor-client hammer [] [] []", 193 NULL, 194 }; 195 196 struct option options[] = { 197 OPT_STRING(0, "token", &token, "token", 198 "command token to send to the server"), 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 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(the_repository); 214 215 if (!strcmp(subcmd, "query")) 216 return !!do_send_query(token); 217 218 if (!strcmp(subcmd, "flush")) 219 return !!do_send_flush(); 220 221 if (!strcmp(subcmd, "hammer")) 222 return !!do_hammer(token, nr_threads, nr_requests); 223 224 die("Unhandled subcommand: '%s'", subcmd); 225 } 226 #endif