void log_init(int second, int log_level) { int ret; struct timezone tz; struct timeval tv; time_info_t ti; /** * */ signal(SIGINT, safe_exit); signal(SIGTERM, safe_exit); signal(SIGQUIT, safe_exit); set_time_out_sec(second); set_min_log_level(log_level); ret = gettimeofday(&tv, &tz); assert(ret == 0); time_zone = -tz.tz_minuteswest/60; int t = tv.tv_sec%(60*60*24); ti.hour = t/(60*60) + time_zone; if(ti.hour >= 24) { ti.hour -= 24; } t %= (60*60); ti.min = t/60; ti.sec = t%60; ti.usec = tv.tv_usec; cur_log_day_num = (tv.tv_sec+time_zone*(60*60))/(60*60*24); getcwd(log_path, MAX_PATH); log_fd = create_new_log(); w_buf = (log_buf_t *)malloc(sizeof(log_buf_t)); r_buf = (log_buf_t *)malloc(sizeof(log_buf_t)); pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); char buf[256]; int count = 0; ret = pthread_create(&log_pid, NULL, write_to_file, NULL); pthread_detach(log_pid); if(ret == -1) { count = sprintf(buf, "[ %02d:%02d:%02d.%06ld ] %-6s %ld %s:%d %s\n", ti.hour, ti.min, ti.sec, ti.usec,\ LOG_ERROR_STR, gettid(), __FILE__, __LINE__, "create async_log thread error"); buf[count] = '\0'; write_to_file_inner(log_fd, buf, count); } else { count = sprintf(buf, "[ %02d:%02d:%02d.%06ld ] %-6s %ld %s:%d %s\n", ti.hour, ti.min, ti.sec, ti.usec, \ LOG_INFO_STR, gettid(), __FILE__, __LINE__, "create async_log thread successfully"); buf[count] = '\0'; write_to_file_inner(log_fd, buf, count); } }
static void *write_to_file(void *arg) { int ret; struct timespec ts; int new_fd = -1; while(1) { pthread_mutex_lock(&mutex); clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += g_time_out_sec; if(r_buf->pos == 0) { ret = pthread_cond_timedwait(&cond, &mutex, &ts); // if(ret == ETIMEDOUT) { // debug_printf("time out\n"); // } else if(ret == 0) { // debug_printf("signaled\n"); // } else { // debug_printf("error in pthread_cond_timedwait\n"); // } if(ret == -1) { fprintf(stderr, "error in pthread_cond_timedwait\n"); } } /** * swap the read buf and write buf */ log_buf_t *tmp = r_buf; r_buf = w_buf; w_buf = tmp; /** * if g_new_fd, create a new log file */ if(g_new_fd) { new_fd = create_new_log(); g_new_fd = 0; } pthread_mutex_unlock(&mutex); if(r_buf->pos) { write_to_file_inner(log_fd, r_buf->buf, r_buf->pos); r_buf->pos = 0; } if(new_fd != -1) { int tmp_fd = log_fd; log_fd = new_fd; close(tmp_fd); } }//end while return NULL; }
static int create_fetcher(struct server *s, int n) { int i; struct fetcher *ws, *tmp; cpu_set_t cpuinfo; pthread_t fpt[FETCHER_NUM]; if (n < 1) return -1; ws = malloc(sizeof(struct fetcher) * n); //associated a worker with main thread if (ws == NULL) return -1; memset(ws, 0, sizeof(struct fetcher) * n); s->fetchers = ws; for (i = 0; i < n; i++) { tmp = ws + i; tmp->s = s; tmp->idx = i; tmp->pkg = 0; tmp->send = 0; tmp->miss = 0; tmp->el = &s->eventlist; tmp->qidx = i % QUIZZER_NUM; tmp->mc = init_msgcache(100); if (tmp->mc == NULL) dns_error(0, "get msgcache"); tmp->loginfo = malloc(sizeof(struct log_info)); memset(tmp->loginfo, 0, sizeof(struct log_info)); tmp->loginfo->lastlog = global_now; tmp->loginfo->log_type = TYPE_FETCHER; tmp->loginfo->logfd = create_new_log(s->logpath, i, TYPE_FETCHER); if (tmp->loginfo->logfd < 0) dns_error(0, "log file error"); if (pthread_create(fpt + i, NULL, (void *) run_fetcher, tmp) != 0) dns_error(0, "init worker"); } global_out_info->thread_num += i; for(i = 0;i < FETCHER_NUM ;i ++) { CPU_ZERO(&cpuinfo); CPU_SET_S(i + 1, sizeof(cpuinfo), &cpuinfo); if(0 != pthread_setaffinity_np(fpt[i], sizeof(cpu_set_t), &cpuinfo)) { printf("set affinity fetcher failed, may be the cpu cores num less than (FETCHER_NUM + QUIZZER_NUM + 1)\n"); // exit(0); } } return 0; }
int create_author(struct server *s, int n) { int i, j; struct author *authors = NULL; cpu_set_t cpuinfo; pthread_t apt[QUIZZER_NUM]; if (n < 1 || n > 50) dns_error(0, "quizzer bad range"); if ((authors = malloc(sizeof(struct author) * n)) == NULL) dns_error(0, "out of memory in quizzer"); memset(authors, 0, sizeof(struct author) * n); s->authors = authors; for (i = 0; i < n; i++) { authors[i].idx = i; authors[i].cudp = s->ludp; authors[i].audp = create_listen_ports(i * 1000 + 998, UDP, NULL); if (authors[i].audp < 0) dns_error(0, "auth fd error"); set_sock_buff(authors[i].audp, 1); authors[i].el = &s->eventlist; authors[i].s = s; get_random_data(authors[i].randombuffer, RANDOM_SIZE); authors[i].rndidx = 0; authors[i].dupbefore = 0; authors[i].limits = 10; authors[i].bdepfd = 0; authors[i].fwd = s->forward; authors[i].ds = s->datasets; authors[i].qnum = 0; authors[i].underattack = 0; authors[i].timex = 0; authors[i].response = 0; authors[i].tcpinuse = 0; authors[i].rdb = 0; authors[i].quizz = 0; authors[i].drop = 0; authors[i].timeout = 0; authors[i].qidx = 0; //start idx in qoutinfo list authors[i].start = QLIST_TABLE_SIZE / QUIZZER_NUM * i; if (i == (QUIZZER_NUM - 1)) authors[i].end = QLIST_TABLE_SIZE; else authors[i].end = QLIST_TABLE_SIZE / QUIZZER_NUM * (i + 1); memset(authors[i].ip, 0, IP_DATA_LEN); authors[i].loginfo = malloc(sizeof(struct log_info)); memset(authors[i].loginfo, 0, sizeof(struct log_info)); authors[i].loginfo->log_type = TYPE_QUIZZER; authors[i].loginfo->logfd = create_new_log(s->logpath, i, TYPE_QUIZZER); for (j = 0; j < AUTH_DB_NUM; j++) pthread_spin_init(&(authors[i].dblock[j]), 0); for (j = 0; j < LIST_SPACE; j++) authors[i].list[j] = NULL; for (j = 0; j < EP_TCP_FDS; j++) authors[i].eptcpfds[j].ret = -1; pthread_spin_init(&authors[i].lock, 0); authors[i].loginfo->lastlog = global_now; if (authors[i].cudp < 0 || authors[i].audp < 0) dns_error(0, "create quizzer2"); if (pthread_create(apt + i, NULL, run_quizzer, (void *) &(authors[i])) != 0) dns_error(0, "create quizzer"); } global_out_info->thread_num += i; for(i = 0;i < QUIZZER_NUM ;i ++) { CPU_ZERO(&cpuinfo); CPU_SET_S(i + FETCHER_NUM + 1, sizeof(cpuinfo), &cpuinfo); if(0 != pthread_setaffinity_np(apt[i], sizeof(cpu_set_t), &cpuinfo)) { printf("set affinity quizzer failed, may be the cpu cores num less than (FETCHER_NUM + QUIZZER_NUM + 1)\n"); // exit(0); } } return 0; }