libev循环在空闲时使用99%的CPU

时间:2016-08-04 22:27:38

标签: c linux libev

所以我有一个运行libev I / O循环和定时器循环的程序。当char数组达到7000个字符或定时器循环达到10秒时,它将转到JSON POST上运行在localhost上的服务。 I / O循环导致程序在空闲时使用几乎100%的CPU。

此程序需要一个1或0的argv:

  • A 1使程序只处理一行并退出。
  • A 0等待输入。

只有在我们传递0并让它等待输入时才会发生错误。

#include <stdio.h>
#include <ev.h>
#include <curl/curl.h>
#include <json-c/json.h>
#include <unistd.h>

void curlPage(char url[], char message[]);
void io_callback(EV_P_ ev_io *w_, int rev);
void time_callback(EV_P_ ev_timer *w_, int rev);

struct watcher
{
    ev_io stdin_watcher;
    ev_timer time_watcher;
};

char lines[BUFSIZ];
char *argv1;
char url[1024] = "http://127.0.0.1:";
char *end;


int main(int argc, char *argv[]) {
    struct ev_loop *loop;
    struct watcher w;

    if (!argv[1]) {
        printf("YOU NEED A 1 OR 0 PARAMATER FOR THIS TO WORK!\n");
        exit(0);
    }
    else {
        end = argv[1];
    }
    argv1 = argv[2];

    if (argv[3]) {
        strcat(url, argv[3]);
    }
    else {
        strcat(url, "8888");
    }

    loop = ev_default_loop(0);

    ev_io_init(&w.stdin_watcher, io_callback, STDIN_FILENO, EV_READ);
    ev_timer_init(&w.time_watcher, time_callback, 10, 0);
    w.time_watcher.repeat=10;
    ev_io_set(&w.stdin_watcher, STDIN_FILENO, EV_READ);
    ev_io_start(loop, &w.stdin_watcher);
    ev_timer_start(loop, &w.time_watcher);

    ev_run(loop, 0); 

    return 0;
}

void time_callback(EV_P_ ev_timer *w_, int rev) {
     if (strlen(lines)) {
         curlPage(url, lines);
         lines[0] = '\0';
     }
     return;
}

void io_callback(EV_P_ ev_io *w_, int rev) {
    struct watcher *w = (struct watcher *)w_;

    char buf[BUFSIZ];
    char * resp;

    resp = fgets(buf, sizeof buf, stdin);
    if (resp != NULL) {
        sprintf(lines, "%s %s", lines, buf);
    }


    if (strlen(lines) > 7000) {
        curlPage(url, lines);
        lines[0] = '\0';
    }
    if (strcmp(end, "1")  == 0) {
        ev_io_stop(loop, w_);
    }
    return;
}

void curlPage(char url[], char message[]) {
    CURL *curl;
    CURLcode res;
    json_object * jsonObj = json_object_new_object();
    char hostname[1024];

    gethostname(hostname, 1024);
    struct curl_slist * headers=NULL;
    headers = curl_slist_append(headers, "Accept: application/json");
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, "charsets: utf-8");

    curl = curl_easy_init();

    if(curl) {

        if (hostname) {
            json_object *jstring2 = json_object_new_string(hostname);
            json_object_object_add(jsonObj, "hostname", jstring2);
        }
        if (argv1) {
            json_object *jstring3 = json_object_new_string(argv1);
            json_object_object_add(jsonObj, "tag", jstring3);
        }

        json_object *jstring = json_object_new_string(message);
        json_object_object_add(jsonObj, "message", jstring);

        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_object_get_string(jsonObj));

        res = curl_easy_perform(curl);

        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_preform() failed: %s\n", curl_easy_strerror(res));
        }
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    json_object_put(jsonObj);

    // run only once. 
    if (strcmp(end, "1")  == 0) {
         exit(0);
    }
    return;
}

这是线程回溯跟踪和堆栈打印输出:

bt and stack print out

因此看起来I / O观察者在第一个事件之后获得连续的I / O事件。它等待第一个事件正确,但之后消耗了大部分CPU。我正在使用它:

cat test.txt | logpush 0 &

也许管道造成了这种情况?

所以我写了一个测试程序,它只是一个简单的libev I / O观察器:

#include <stdio.h>
#include <ev.h>
#include <unistd.h>

void io_callback(EV_P_ ev_io *w_, int rev);
void time_callback(EV_P_ ev_timer *w_, int rev);

char lines[BUFSIZ];

int main(int argc, char *argv[]) {
    struct ev_loop *loop;
    struct ev_io stdin_watcher;

    loop = ev_default_loop(0);

    ev_io_init(&stdin_watcher, io_callback, STDIN_FILENO, EV_READ);
    ev_io_set(&stdin_watcher, STDIN_FILENO, EV_READ);
    ev_io_start(loop, &stdin_watcher);

    ev_run(loop, 0);

    return 0;
}

void io_callback(EV_P_ ev_io *w_, int rev) {
    printf("callback hit\n");
    return;
}

如果使用管道调用没有输入,I / O回调每秒会被命中几百次,如下所示:

cat test.txt | ./test &

当我将进程的stdout传递给我的程序时,也会发生这种情况。

这是我的问题的根本原因。

2 个答案:

答案 0 :(得分:0)

sprintf(lines, "%s %s", lines, buf);是未定义的行为。

int sprintf(char * restrict s, const char * restrict format, ...);

restrict表示sprintf()不希望数据s指向该函数访问的任何其他内容。由于代码将lines传递给s和一个参数,代码破坏了合同和未定义的行为结果。可能的lines是简单的成长,成长和成长。

相反

// sprintf(lines, "%s %s", lines, buf);
strcat(lines, " ");
strcat(lines, buf);
// Other more time efficient code is possible

像往常一样,注意缓冲区溢出是一个问题。

代码也可能有其他问题。

答案 1 :(得分:0)

第二个代码段的修改适用于管道(至少:cat event.c | ./a.out)。问题似乎是没有检测到EOF,并继续锤击filedescriptor

#include <stdio.h>
#include <ev.h>
#include <unistd.h>

void io_callback(EV_P_ ev_io *w_, int rev);
void time_callback(EV_P_ ev_timer *w_, int rev);


struct ev_loop *loop; /* made this global, because needed by the callback at closing time */

int main(int argc, char *argv[]) {
    struct ev_io stdin_watcher;

    loop = ev_default_loop(0);

    ev_io_init(&stdin_watcher, io_callback, STDIN_FILENO, EV_READ);
    ev_io_set(&stdin_watcher, STDIN_FILENO, EV_READ);
    ev_io_start(loop, &stdin_watcher);

    // ev_run(loop, 0);
    ev_loop(loop, 0);

    return 0;
}

void io_callback(EV_P_ ev_io *w_, int rev) {
    int rc;
    char ch;

        /* replaced fgets() by read() */
    rc = read(STDIN_FILENO, &ch, 1);

        /* diagnostic output should go to stderr */
    fprintf(stderr, "Callback hit, rc=%d, ch = %02x\n"
        , rc, ch & 0xff
        );
    if (rc == 0) ev_io_stop(loop, w_);
    return;
}

- 为了进行测试,您可以使用以下程序并通过二进制管道输出,例如:sh loop.sh | ./a.out

#!/bin/sh
while true; do
        date
        sleep 5
done