From patchwork Wed Oct 26 13:18:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Eckert X-Patchwork-Id: 1694938 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Hb9PYlTE; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4My8cX1vDcz23js for ; Thu, 27 Oct 2022 00:22:35 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=4jfAFH3TI5y+I8wYXITh+fsHtEk/1tq6qpLUl0W4aBQ=; b=Hb9PYlTEE73Vvw XMxESNyUE0jRu98LqA+fQVlYcg+trnv0ji1qZdqkqaLEpCqDHqvkoE82H6XMwKl1nE5cjCeTh8R2G fBf5TrH8BTHIv7BQg1thkar8PFofjEtFL2bp98UXkwlWlhLXSIcly0uK2XtgdQGyJlf5LZfDmMZda JRbYPonIy1DJX6z/eUCT+q5sjhG6rYcViPrpv0ZJkKEY3klGhE2CCZmg2TuEe0ngs7QJR/+rGOEua Y6J+kAH5yBjYCopu8PwbhFUTRMZYuZqhYANUmmxSM6xXjIywTm7pJfrkXw58sm4L4AemiOctLmKet Qb3UrTfwmARGjnLZftPg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ongJH-009S0s-KU; Wed, 26 Oct 2022 13:19:07 +0000 Received: from mxout70.expurgate.net ([194.37.255.70]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ongJC-009Ryw-Vz for openwrt-devel@lists.openwrt.org; Wed, 26 Oct 2022 13:19:04 +0000 Received: from [127.0.0.1] (helo=localhost) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1ongJ6-000H1i-LL; Wed, 26 Oct 2022 15:18:56 +0200 Received: from [195.243.126.94] (helo=securemail.tdt.de) by relay.expurgate.net with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ongJ5-000I9V-TT; Wed, 26 Oct 2022 15:18:55 +0200 Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id 92666240049; Wed, 26 Oct 2022 15:18:55 +0200 (CEST) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id 37D1F240040; Wed, 26 Oct 2022 15:18:55 +0200 (CEST) Received: from localhost.localdomain (unknown [10.2.3.40]) by mail.dev.tdt.de (Postfix) with ESMTPSA id 98E8128E4D; Wed, 26 Oct 2022 15:18:54 +0200 (CEST) From: Florian Eckert To: daniel@makrotopia.org, ynezz@true.cz Cc: Eckert.Florian@googlemail.com, openwrt-devel@lists.openwrt.org Subject: [PATCH procd] ubus: add state measurement Date: Wed, 26 Oct 2022 15:18:51 +0200 Message-ID: <20221026131851.644176-1-fe@dev.tdt.de> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.dev.tdt.de X-purgate-ID: 151534::1666790336-5627986A-733CBED4/0/0 X-purgate: clean X-purgate-type: clean X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221026_061903_239855_2D719C02 X-CRM114-Status: GOOD ( 21.35 ) X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Procd has different states during booting. When the system is booted, it is in the 'running' state. This state is only exited when the system is shut down cleanly. This state is called 'shutdown'. To [...] Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [194.37.255.70 listed in list.dnswl.org] 0.0 SPF_NONE SPF: sender does not publish an SPF Record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Procd has different states during booting. When the system is booted, it is in the 'running' state. This state is only exited when the system is shut down cleanly. This state is called 'shutdown'. To find out what state the system is in and how long it will take to complete this, the commit adds a new section 'state' to the ubus call system info. There you can read which state the procd is in and how long it has been in this state or how long it has been running in the state. command: ubus call system info output: { "localtime": 1666795909, "state": { "load": { "active": false, "duration": 42.667914 }, "early": { "active": false, "duration": 1.107519 }, "ubus": { "active": false, "duration": 0.536634 }, "init": { "active": false, "duration": 123.176279 }, "running": { "active": true, "duration": 226.491805 }, "shutdown": { "active": false, "duration": 0.000000 } }, Signed-off-by: Florian Eckert --- This is a followup of pullrequest with the proposed changes. https://github.com/openwrt/openwrt/pull/10937 procd.h | 2 ++ state.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ system.c | 2 ++ 3 files changed, 86 insertions(+) diff --git a/procd.h b/procd.h index fd29a12..3299b41 100644 --- a/procd.h +++ b/procd.h @@ -15,6 +15,7 @@ #ifndef __PROCD_H #define __PROCD_H +#include #include #include #include @@ -36,6 +37,7 @@ void ubus_init_system(struct ubus_context *ctx); void procd_state_next(void); void procd_state_ubus_connect(void); +void procd_state_event(struct blob_buf *b); void procd_shutdown(int event); void procd_early(void); void procd_preinit(void); diff --git a/state.c b/state.c index fb81248..acbcece 100644 --- a/state.c +++ b/state.c @@ -13,6 +13,7 @@ */ #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include #include +#include #include "container.h" #include "procd.h" @@ -43,6 +45,22 @@ enum { static int state = STATE_NONE; static int reboot_event; +struct state_event { + struct timespec ts_event; + bool active; + const char *name; +}; + +static struct state_event s_event[__STATE_MAX] = { + [STATE_NONE] = {{0, 0}, true, "load" }, + [STATE_EARLY] = {{0, 0}, false, "early" }, + [STATE_UBUS] = {{0, 0}, false, "ubus" }, + [STATE_INIT] = {{0, 0}, false, "init" }, + [STATE_RUNNING] = {{0, 0}, false, "running" }, + [STATE_SHUTDOWN] = {{0, 0}, false, "shutdown" }, + [STATE_HALT] = {{0, 0}, false, "halt" }, +}; + static void set_stdio(const char* tty) { if (chdir("/dev") || @@ -123,11 +141,23 @@ static void perform_halt() sleep(1); } +static void update_state_event(void) +{ + // set previous state inactive + s_event[state-1].active = false; + + s_event[state].active = true; + clock_gettime(CLOCK_BOOTTIME, &s_event[state].ts_event); +} + static void state_enter(void) { char ubus_cmd[] = "/sbin/ubusd"; struct passwd *p; + if (state > STATE_NONE && state < __STATE_MAX) + update_state_event(); + switch (state) { case STATE_EARLY: LOG("- early -\n"); @@ -228,3 +258,55 @@ void procd_shutdown(int event) state = STATE_SHUTDOWN; state_enter(); } + +void procd_state_event(struct blob_buf *b) +{ + void *c, *s; + struct timespec *ts_start; + struct timespec *ts_stop; + struct timespec ts_active; + struct timespec ts_res; + double duration; + + c = blobmsg_open_table(b, "state"); + + for (int i = 0; i < __STATE_MAX - 1; i++) + { + ts_start = &s_event[i].ts_event; + ts_stop = &s_event[i+1].ts_event; + + if (ts_stop->tv_sec > 0) { + ts_res.tv_sec = ts_stop->tv_sec - ts_start->tv_sec; + ts_res.tv_nsec = ts_stop->tv_nsec - ts_start->tv_nsec; + } + else if (s_event[i].active) { + clock_gettime(CLOCK_BOOTTIME, &ts_active); + ts_res.tv_sec = ts_active.tv_sec - ts_start->tv_sec; + ts_res.tv_nsec = ts_active.tv_nsec - ts_start->tv_nsec; + } + else { + ts_res.tv_sec = 0; + ts_res.tv_nsec = 0; + } + + // correct overflow calculation + if (ts_res.tv_nsec < 0) { + --ts_res.tv_sec; + ts_res.tv_nsec += 1000000000L; + } + if ( ts_res.tv_sec < 0) { + ts_res.tv_sec = ts_start->tv_sec; + ts_res.tv_nsec = ts_start->tv_nsec; + } + + #define NSEC_PER_SEC 1000000000 + duration = ((double)(ts_res.tv_sec) + ((double)(ts_res.tv_nsec) / NSEC_PER_SEC)); + + s = blobmsg_open_table(b, s_event[i].name); + blobmsg_add_u8(b, "active", s_event[i].active); + blobmsg_add_double(b, "duration", duration); + blobmsg_close_table(b, s); + } + + blobmsg_close_table(b, c); +} diff --git a/system.c b/system.c index 93eac59..43c2e94 100644 --- a/system.c +++ b/system.c @@ -376,6 +376,8 @@ static int system_info(struct ubus_context *ctx, struct ubus_object *obj, blobmsg_add_u32(&b, "localtime", now + tm->tm_gmtoff); + procd_state_event(&b); + #ifdef linux blobmsg_add_u32(&b, "uptime", info.uptime);