Changeset 10
- Timestamp:
- Nov 2, 2005 5:38:33 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/main/pjlib/src/pjlib-test/udp_echo_srv_sync.c
r6 r10 4 4 #include <pjlib.h> 5 5 6 static pj_sem_t *sem; 7 static pj_mutex_t *mutex; 8 static pj_size_t total_bw; 6 static pj_atomic_t *total_bytes; 9 7 10 8 static int worker_thread(void *arg) … … 12 10 pj_sock_t sock = (pj_sock_t)arg; 13 11 char buf[1516]; 14 pj_size_t received;15 pj_time_val last_print;16 12 pj_status_t last_recv_err = PJ_SUCCESS, last_write_err = PJ_SUCCESS; 17 18 received = 0;19 pj_gettimeofday(&last_print);20 13 21 14 for (;;) { 22 15 pj_ssize_t len; 23 pj_uint32_t delay_msec;24 pj_time_val now;25 pj_highprec_t bw;26 16 pj_status_t rc; 27 17 pj_sockaddr_in addr; … … 39 29 } 40 30 41 received += len;31 pj_atomic_add(total_bytes, len); 42 32 43 33 rc = pj_sock_sendto(sock, buf, &len, 0, &addr, addrlen); … … 49 39 continue; 50 40 } 51 52 pj_gettimeofday(&now);53 PJ_TIME_VAL_SUB(now, last_print);54 delay_msec = PJ_TIME_VAL_MSEC(now);55 56 if (delay_msec < 1000)57 continue;58 59 bw = received;60 pj_highprec_mul(bw, 1000);61 pj_highprec_div(bw, delay_msec);62 63 pj_mutex_lock(mutex);64 total_bw = total_bw + (pj_size_t)bw;65 pj_mutex_unlock(mutex);66 67 pj_gettimeofday(&last_print);68 received = 0;69 pj_sem_post(sem);70 pj_thread_sleep(0);71 41 } 72 42 } … … 79 49 pj_thread_t *thread[ECHO_SERVER_MAX_THREADS]; 80 50 pj_status_t rc; 81 pj_highprec_t abs_total; 51 pj_highprec_t last_received, avg_bw, highest_bw; 52 pj_time_val last_print; 82 53 unsigned count; 83 54 int i; … … 87 58 return -5; 88 59 89 rc = pj_ sem_create(pool, NULL, 0, ECHO_SERVER_MAX_THREADS, &sem);60 rc = pj_atomic_create(pool, 0, &total_bytes); 90 61 if (rc != PJ_SUCCESS) { 91 app_perror("...unable to create semaphore", rc);62 app_perror("...unable to create atomic_var", rc); 92 63 return -6; 93 }94 95 rc = pj_mutex_create_simple(pool, NULL, &mutex);96 if (rc != PJ_SUCCESS) {97 app_perror("...unable to create mutex", rc);98 return -7;99 64 } 100 65 … … 119 84 PJ_LOG(3,("", "...Press Ctrl-C to abort")); 120 85 121 abs_total = 0; 86 last_received = 0; 87 pj_gettimeofday(&last_print); 88 avg_bw = highest_bw = 0; 122 89 count = 0; 123 90 124 91 for (;;) { 125 pj_uint32_t avg32; 126 pj_highprec_t avg; 92 pj_highprec_t received, cur_received, bw; 93 unsigned msec; 94 pj_time_val now, duration; 127 95 128 for (i=0; i<ECHO_SERVER_MAX_THREADS; ++i) 129 pj_sem_wait(sem); 96 pj_thread_sleep(1000); 130 97 131 /* calculate average so far: 132 avg = abs_total / count; 133 */ 98 received = cur_received = pj_atomic_get(total_bytes); 99 cur_received = cur_received - last_received; 100 101 pj_gettimeofday(&now); 102 duration = now; 103 PJ_TIME_VAL_SUB(duration, last_print); 104 msec = PJ_TIME_VAL_MSEC(duration); 105 106 bw = cur_received; 107 pj_highprec_mul(bw, 1000); 108 pj_highprec_div(bw, msec); 109 110 last_print = now; 111 last_received = received; 112 113 avg_bw = avg_bw + bw; 134 114 count++; 135 abs_total += total_bw;136 avg = abs_total;137 pj_highprec_div(avg, count);138 avg32 = (pj_uint32_t)avg;139 115 140 141 116 PJ_LOG(3,("", "Synchronous UDP (%d threads): %u KB/s (avg=%u KB/s) %s", 142 117 ECHO_SERVER_MAX_THREADS, 143 total_bw / 1000,144 avg32 / 1000,118 (unsigned)(bw / 1000), 119 (unsigned)(avg_bw / count / 1000), 145 120 (count==20 ? "<ses avg>" : ""))); 146 121 147 total_bw = 0; 122 if (count==20) { 123 if (avg_bw/count > highest_bw) 124 highest_bw = avg_bw/count; 148 125 149 if (count==20) {150 126 count = 0; 151 abs_total = 0; 127 avg_bw = 0; 128 129 PJ_LOG(3,("", "Highest average bandwidth=%u KB/s", 130 (unsigned)(highest_bw/1000))); 152 131 } 153 154 while (pj_sem_trywait(sem) == PJ_SUCCESS)155 ;156 132 } 157 133 }
Note: See TracChangeset
for help on using the changeset viewer.