loadtest: counter position is now derived from thread id, not socket number.
authorArt Cancro <ajc@citadel.org>
Mon, 16 Oct 2023 19:53:17 +0000 (19:53 +0000)
committerArt Cancro <ajc@citadel.org>
Mon, 16 Oct 2023 19:53:17 +0000 (19:53 +0000)
This is what I was actually trying to accomplish a couple of commits ago.  I made
a race condition and that's why it was malfunctioning.  This way is better and it
works.  It also once shot a man in Reno just to watch him die.

citadel/utils/loadtest.c

index 0f5920ab4c08adcc92fb91cafc1e4907cce6d716..d24977ccd33efef2bdd16e8e184448652acac8ef 100644 (file)
@@ -275,10 +275,12 @@ void perform_random_thing(int serv_sock) {
 
 
 // This is the main loop.  We log in as the load test user, and then perform random operations until stopped.
-void *loadtest(void *blah) {
+void *loadtest(void *pointer_to_thread_id) {
        char buf[SIZ];
        int serv_sock;
 
+       int thread_id = *(int *)pointer_to_thread_id;
+
        serv_sock = uds_connectsock(file_citadel_socket);
 
        if (serv_sock < 0) {
@@ -299,8 +301,8 @@ void *loadtest(void *blah) {
        serv_gets(serv_sock, buf);
 
        // Find a nice spot on the screen to show the operation count for this thread.
-       int row = 10 + ((serv_sock-3) % 20);
-       int col = ((serv_sock-3) / 20) * 10;
+       int row = 10 + (thread_id % 20);
+       int col = (thread_id / 20) * 10;
        long ops = 0;
        printf("\033[%d;%dH\033[33m       0\033[0m", row, col);
        fflush(stdout);
@@ -384,7 +386,7 @@ int main(int argc, char **argv) {
        for (i=0; i<nthreads; ++i) {
                row = 10 + (i % 20);
                col = (i / 20) * 10;
-               printf("\033[%d;%dH\033[31m--------\033[0m", row, col);
+               printf("\033[%d;%dH\033[31m       -\033[0m", row, col);
                fflush(stdout);
        }
 
@@ -400,7 +402,12 @@ int main(int argc, char **argv) {
        setup_accounts(serv_sock);
        close(serv_sock);
 
-       for (i=0; i<(nthreads-1); ++i) {
+       size_t * threadId = calloc(nthreads, sizeof(size_t));
+       for (size_t i = 0; i < nthreads; ++i) {
+               threadId[i] = i;
+       }
+
+       for (i=1; i<nthreads; ++i) {
 
                pthread_t thread;
                pthread_attr_t attr;
@@ -408,12 +415,12 @@ int main(int argc, char **argv) {
 
                ret = pthread_attr_init(&attr);
                ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
-               ret = pthread_create(&thread, &attr, loadtest, NULL);
+               ret = pthread_create(&thread, &attr, loadtest, &threadId[i]);
                if (ret != 0) {
                        exit(ret);
                }
 
        }
-       loadtest(NULL);
+       loadtest(&threadId[0]);
        return(0);
 }