所以我有一个运行 libev I/O 循环和计时器循环的程序。当 char 数组达到 7000 个字符或计时器循环达到 10 秒时,它会转到 JSON POST 一个在 localhost 上运行的服务。I/O 循环导致程序在空闲时使用几乎 100% 的 CPU。
这个程序需要一个 1 或 0 的 argv:
- A 1 使程序只处理一行并退出。
- 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;
}
这是线程回溯和堆栈打印输出:
所以看起来 I/O 观察者在第一个事件之后获得了连续的 I/O 事件。它会正确等待第一个事件,但之后会消耗大部分 CPU。我这样使用它:
cat test.txt | logpush 0 &
也许是管道导致了这种情况?
所以我写了一个测试程序,它只是一个简单的 libev I/O watcher:
#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 &
当我将进程的标准输出通过管道传输到我的程序时,也会发生这种情况。
这是我的问题的根本原因。