Skip to content

Instantly share code, notes, and snippets.

@ideawu
Last active December 3, 2020 01:30
Show Gist options
  • Save ideawu/f89f39352c0f295267e658ba2f418109 to your computer and use it in GitHub Desktop.
Save ideawu/f89f39352c0f295267e658ba2f418109 to your computer and use it in GitHub Desktop.
Test the latency of signal epoll_wait() to return by kill()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/epoll.h>
#include <pthread.h>
#include <signal.h>
double microtime(){
struct timeval now;
gettimeofday(&now, NULL);
double ret = now.tv_sec + now.tv_usec/1000.0/1000.0;
return ret;
}
#define MAX_EVENTS 10
struct epoll_event ev, events[MAX_EVENTS];
int epollfd;
int signaled = 0;
double sig_time = 0;
void signal_handler(int sig){
// fprintf(stderr, "%d %.6f signal_handler\n", __LINE__, microtime());
}
void* run_thread(void *arg){
while(1){
sleep(1);
//fprintf(stderr, "%d %.6f signal 1\n", __LINE__, microtime());
kill(getpid(), SIGALRM);
signaled = 1;
sig_time = microtime();
//fprintf(stderr, "%d %.6f signal 2\n", __LINE__, microtime());
}
return (void *)NULL;
}
void init(){
epollfd = epoll_create(1024);
if (epollfd == -1) {
perror("epoll_create");
exit(EXIT_FAILURE);
}
signal(SIGALRM, signal_handler);
pthread_t tid;
int err = pthread_create(&tid, NULL, run_thread, NULL);
}
int main(int argc, char **argv){
init();
while(1){
int nfds = epoll_wait(epollfd, events, MAX_EVENTS, 10);
if(signaled){
signaled = 0;
double latency = microtime() - sig_time;
fprintf(stderr, "%d %.6f latency = %.6f\n", __LINE__, microtime(), latency);
}
//fprintf(stderr, "%d %.6f epoll_wait\n", __LINE__, microtime());
if (nfds == -1) {
if(errno == EINTR){
continue;
}
perror("epoll_wait");
exit(EXIT_FAILURE);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment