gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:39 26 4
gpt4 key购买 nike

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

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

  • 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;
}

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

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 &

当我将进程的标准输出通过管道传输到我的程序时,也会发生这种情况。

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

最佳答案

第二个片段的修改适用于管道(至少: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

关于c - libev 循环在空闲时使用 99% 的 CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38778375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com