Skip to content

Commit

Permalink
Print stats when interrupted by SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
seriyps committed Sep 22, 2012
1 parent 240278a commit 1d3c5dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/wrk.c
Expand Up @@ -10,6 +10,7 @@
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -51,6 +52,9 @@ static struct {
pthread_mutex_t mutex;
} statistics;

/* global for signal handling */
static thread *threads;

static const struct http_parser_settings parser_settings = {
.on_message_complete = request_complete
};
Expand All @@ -73,6 +77,7 @@ static void usage() {
int main(int argc, char **argv) {
struct addrinfo *addrs, *addr;
struct http_parser_url parser_url;
struct sigaction sigint_action;
char *url, **headers;
int rc;

Expand Down Expand Up @@ -218,7 +223,7 @@ int main(int argc, char **argv) {
statistics.latency = stats_alloc(SAMPLES);
statistics.requests = stats_alloc(SAMPLES);

thread *threads = zcalloc(cfg.threads * sizeof(thread));
threads = zcalloc(cfg.threads * sizeof(thread));
uint64_t connections = cfg.connections / cfg.threads;
uint64_t requests_cnt = cfg.requests / cfg.threads;

Expand All @@ -242,6 +247,13 @@ int main(int argc, char **argv) {
uint64_t bytes = 0;
errors errors = { 0 };

sigint_action.sa_handler = &sig_handler;
sigemptyset (&sigint_action.sa_mask);
/* reset handler in case when pthread_cancel didn't stop
threads for some reason */
sigint_action.sa_flags = SA_RESETHAND;
sigaction(SIGINT, &sigint_action, NULL);

for (uint64_t i = 0; i < cfg.threads; i++) {
thread *t = &threads[i];

Expand Down Expand Up @@ -287,6 +299,14 @@ int main(int argc, char **argv) {
return 0;
}

/* Stop threads, so main thread can print stats when join's return */
static void sig_handler(int signum) {
printf("interrupted\n");
for (uint64_t i = 0; i < cfg.threads; i++) {
if(pthread_cancel(threads[i].thread)) exit(1);
}
}

void *thread_main(void *arg) {
thread *thread = arg;

Expand Down Expand Up @@ -595,6 +615,7 @@ static void progress_report(thread *threads){
printf("Completed %"PRIu64" requests\n", complete);
}

/* Will call progress_report every 5 seconds until the thread is finished */
static int await_thread(pthread_t t, thread *threads) {
struct timespec ts;
int s;
Expand Down
1 change: 1 addition & 0 deletions src/wrk.h
Expand Up @@ -53,6 +53,7 @@ typedef struct connection {

struct config;

static void sig_handler(int);
static void *thread_main(void *);
static int connect_socket(thread *, connection *);
static int reconnect_socket(thread *, connection *);
Expand Down

0 comments on commit 1d3c5dd

Please sign in to comment.