From nobody Mon Nov 25 05:25:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1717509139; cv=none; d=zohomail.com; s=zohoarc; b=Z5CPe0luqE9PIWK3f5F83/T3zSEcKBFc4x3EnyMo1sBlH+AeNT6TJppZ6qBH2qG7RaGyY5y6QrI22avZ37xGFScOgthS+hSuoFRzwTqcFP6dhXl8qRi8xk0KB0Zs/TKGVf4YrepsRvHmrubuGinMLyL51wJWAgasKPP2Y/yZJGQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1717509139; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YF4Tr2vYM9PIGmA0EYLKAkgGXhIMkxzWAPZz1BH/h8o=; b=iamk8nzDqcFkvodKMR/Jj9zG75LkiTed8+tkNBqSAYkgd801iDe4/4YpFww3RGCvCGiFVjDMerTv9oDPViZd3vsehFk9YuFu8yw2WGhKn3hYJZvL6LbRg3p3wKUj81NzuU/yjSGeSkTsE2o6vJm0AlBotQ9UbChbbcl0uu9ZaUc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1717509139929265.2754362707917; Tue, 4 Jun 2024 06:52:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sEUXz-0005oe-MV; Tue, 04 Jun 2024 09:49:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sEUXx-0005nR-ON for qemu-devel@nongnu.org; Tue, 04 Jun 2024 09:49:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sEUXv-0002lu-Ge for qemu-devel@nongnu.org; Tue, 04 Jun 2024 09:49:53 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-193-G95gRGRcPJWQimLJPjcZ0A-1; Tue, 04 Jun 2024 09:49:48 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 037FD3C00085; Tue, 4 Jun 2024 13:49:48 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.39.194.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E893C15C46; Tue, 4 Jun 2024 13:49:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717508990; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YF4Tr2vYM9PIGmA0EYLKAkgGXhIMkxzWAPZz1BH/h8o=; b=F8qFjxJZswnevjdkgDpjJSdfohH5I8Awb5SKiaFCA9QlRY0L62NkapZO/nStEL15GPc0rT mt4jYz9tyUttJbQQkKmXxwa+4gufet2ag6froXqDzuL4eFA7uNF/sCGpz/I0JLEESDQn+O sdQuFLJNDjjk5yXCdAL+qSfJ11k//2c= X-MC-Unique: G95gRGRcPJWQimLJPjcZ0A-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Thomas Huth , Konstantin Kostiuk , Michael Roth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [PATCH 05/20] qga: move linux disk/cpu stats command impls to commands-linux.c Date: Tue, 4 Jun 2024 14:49:18 +0100 Message-ID: <20240604134933.220112-6-berrange@redhat.com> In-Reply-To: <20240604134933.220112-1-berrange@redhat.com> References: <20240604134933.220112-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1717509141470100001 The qmp_guest_{diskstats,cpustats} command impls in commands-posix.c are surrounded by '#ifdef __linux__' so should instead live in commands-linux.c This also removes a "#ifdef CONFIG_LINUX" that was nested inside a "#ifdef __linux__". Signed-off-by: Daniel P. Berrang=C3=A9 --- qga/commands-linux.c | 195 ++++++++++++++++++++++++++++++++++++++++++ qga/commands-posix.c | 199 ------------------------------------------- 2 files changed, 195 insertions(+), 199 deletions(-) diff --git a/qga/commands-linux.c b/qga/commands-linux.c index 084e6c9e85..c0e8bd4062 100644 --- a/qga/commands-linux.c +++ b/qga/commands-linux.c @@ -1594,3 +1594,198 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorLi= st *vcpus, Error **errp) =20 return processed; } + +#define MAX_NAME_LEN 128 +static GuestDiskStatsInfoList *guest_get_diskstats(Error **errp) +{ + GuestDiskStatsInfoList *head =3D NULL, **tail =3D &head; + const char *diskstats =3D "/proc/diskstats"; + FILE *fp; + size_t n; + char *line =3D NULL; + + fp =3D fopen(diskstats, "r"); + if (fp =3D=3D NULL) { + error_setg_errno(errp, errno, "open(\"%s\")", diskstats); + return NULL; + } + + while (getline(&line, &n, fp) !=3D -1) { + g_autofree GuestDiskStatsInfo *diskstatinfo =3D NULL; + g_autofree GuestDiskStats *diskstat =3D NULL; + char dev_name[MAX_NAME_LEN]; + unsigned int ios_pgr, tot_ticks, rq_ticks, wr_ticks, dc_ticks, fl_= ticks; + unsigned long rd_ios, rd_merges_or_rd_sec, rd_ticks_or_wr_sec, wr_= ios; + unsigned long wr_merges, rd_sec_or_wr_ios, wr_sec; + unsigned long dc_ios, dc_merges, dc_sec, fl_ios; + unsigned int major, minor; + int i; + + i =3D sscanf(line, "%u %u %s %lu %lu %lu" + "%lu %lu %lu %lu %u %u %u %u" + "%lu %lu %lu %u %lu %u", + &major, &minor, dev_name, + &rd_ios, &rd_merges_or_rd_sec, &rd_sec_or_wr_ios, + &rd_ticks_or_wr_sec, &wr_ios, &wr_merges, &wr_sec, + &wr_ticks, &ios_pgr, &tot_ticks, &rq_ticks, + &dc_ios, &dc_merges, &dc_sec, &dc_ticks, + &fl_ios, &fl_ticks); + + if (i < 7) { + continue; + } + + diskstatinfo =3D g_new0(GuestDiskStatsInfo, 1); + diskstatinfo->name =3D g_strdup(dev_name); + diskstatinfo->major =3D major; + diskstatinfo->minor =3D minor; + + diskstat =3D g_new0(GuestDiskStats, 1); + if (i =3D=3D 7) { + diskstat->has_read_ios =3D true; + diskstat->read_ios =3D rd_ios; + diskstat->has_read_sectors =3D true; + diskstat->read_sectors =3D rd_merges_or_rd_sec; + diskstat->has_write_ios =3D true; + diskstat->write_ios =3D rd_sec_or_wr_ios; + diskstat->has_write_sectors =3D true; + diskstat->write_sectors =3D rd_ticks_or_wr_sec; + } + if (i >=3D 14) { + diskstat->has_read_ios =3D true; + diskstat->read_ios =3D rd_ios; + diskstat->has_read_sectors =3D true; + diskstat->read_sectors =3D rd_sec_or_wr_ios; + diskstat->has_read_merges =3D true; + diskstat->read_merges =3D rd_merges_or_rd_sec; + diskstat->has_read_ticks =3D true; + diskstat->read_ticks =3D rd_ticks_or_wr_sec; + diskstat->has_write_ios =3D true; + diskstat->write_ios =3D wr_ios; + diskstat->has_write_sectors =3D true; + diskstat->write_sectors =3D wr_sec; + diskstat->has_write_merges =3D true; + diskstat->write_merges =3D wr_merges; + diskstat->has_write_ticks =3D true; + diskstat->write_ticks =3D wr_ticks; + diskstat->has_ios_pgr =3D true; + diskstat->ios_pgr =3D ios_pgr; + diskstat->has_total_ticks =3D true; + diskstat->total_ticks =3D tot_ticks; + diskstat->has_weight_ticks =3D true; + diskstat->weight_ticks =3D rq_ticks; + } + if (i >=3D 18) { + diskstat->has_discard_ios =3D true; + diskstat->discard_ios =3D dc_ios; + diskstat->has_discard_merges =3D true; + diskstat->discard_merges =3D dc_merges; + diskstat->has_discard_sectors =3D true; + diskstat->discard_sectors =3D dc_sec; + diskstat->has_discard_ticks =3D true; + diskstat->discard_ticks =3D dc_ticks; + } + if (i >=3D 20) { + diskstat->has_flush_ios =3D true; + diskstat->flush_ios =3D fl_ios; + diskstat->has_flush_ticks =3D true; + diskstat->flush_ticks =3D fl_ticks; + } + + diskstatinfo->stats =3D g_steal_pointer(&diskstat); + QAPI_LIST_APPEND(tail, diskstatinfo); + diskstatinfo =3D NULL; + } + free(line); + fclose(fp); + return head; +} + +GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp) +{ + return guest_get_diskstats(errp); +} + +GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) +{ + GuestCpuStatsList *head =3D NULL, **tail =3D &head; + const char *cpustats =3D "/proc/stat"; + int clk_tck =3D sysconf(_SC_CLK_TCK); + FILE *fp; + size_t n; + char *line =3D NULL; + + fp =3D fopen(cpustats, "r"); + if (fp =3D=3D NULL) { + error_setg_errno(errp, errno, "open(\"%s\")", cpustats); + return NULL; + } + + while (getline(&line, &n, fp) !=3D -1) { + GuestCpuStats *cpustat =3D NULL; + GuestLinuxCpuStats *linuxcpustat; + int i; + unsigned long user, system, idle, iowait, irq, softirq, steal, gue= st; + unsigned long nice, guest_nice; + char name[64]; + + i =3D sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", + name, &user, &nice, &system, &idle, &iowait, &irq, &sof= tirq, + &steal, &guest, &guest_nice); + + /* drop "cpu 1 2 3 ...", get "cpuX 1 2 3 ..." only */ + if ((i =3D=3D EOF) || strncmp(name, "cpu", 3) || (name[3] =3D=3D '= \0')) { + continue; + } + + if (i < 5) { + slog("Parsing cpu stat from %s failed, see \"man proc\"", cpus= tats); + break; + } + + cpustat =3D g_new0(GuestCpuStats, 1); + cpustat->type =3D GUEST_CPU_STATS_TYPE_LINUX; + + linuxcpustat =3D &cpustat->u.q_linux; + linuxcpustat->cpu =3D atoi(&name[3]); + linuxcpustat->user =3D user * 1000 / clk_tck; + linuxcpustat->nice =3D nice * 1000 / clk_tck; + linuxcpustat->system =3D system * 1000 / clk_tck; + linuxcpustat->idle =3D idle * 1000 / clk_tck; + + if (i > 5) { + linuxcpustat->has_iowait =3D true; + linuxcpustat->iowait =3D iowait * 1000 / clk_tck; + } + + if (i > 6) { + linuxcpustat->has_irq =3D true; + linuxcpustat->irq =3D irq * 1000 / clk_tck; + linuxcpustat->has_softirq =3D true; + linuxcpustat->softirq =3D softirq * 1000 / clk_tck; + } + + if (i > 8) { + linuxcpustat->has_steal =3D true; + linuxcpustat->steal =3D steal * 1000 / clk_tck; + } + + if (i > 9) { + linuxcpustat->has_guest =3D true; + linuxcpustat->guest =3D guest * 1000 / clk_tck; + } + + if (i > 10) { + linuxcpustat->has_guest =3D true; + linuxcpustat->guest =3D guest * 1000 / clk_tck; + linuxcpustat->has_guestnice =3D true; + linuxcpustat->guestnice =3D guest_nice * 1000 / clk_tck; + } + + QAPI_LIST_APPEND(tail, cpustat); + } + + free(line); + fclose(fp); + return head; +} diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 98aafc45f3..5da60e65ab 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1195,205 +1195,6 @@ GuestMemoryBlockInfo *qmp_guest_get_memory_block_in= fo(Error **errp) return info; } =20 -#define MAX_NAME_LEN 128 -static GuestDiskStatsInfoList *guest_get_diskstats(Error **errp) -{ -#ifdef CONFIG_LINUX - GuestDiskStatsInfoList *head =3D NULL, **tail =3D &head; - const char *diskstats =3D "/proc/diskstats"; - FILE *fp; - size_t n; - char *line =3D NULL; - - fp =3D fopen(diskstats, "r"); - if (fp =3D=3D NULL) { - error_setg_errno(errp, errno, "open(\"%s\")", diskstats); - return NULL; - } - - while (getline(&line, &n, fp) !=3D -1) { - g_autofree GuestDiskStatsInfo *diskstatinfo =3D NULL; - g_autofree GuestDiskStats *diskstat =3D NULL; - char dev_name[MAX_NAME_LEN]; - unsigned int ios_pgr, tot_ticks, rq_ticks, wr_ticks, dc_ticks, fl_= ticks; - unsigned long rd_ios, rd_merges_or_rd_sec, rd_ticks_or_wr_sec, wr_= ios; - unsigned long wr_merges, rd_sec_or_wr_ios, wr_sec; - unsigned long dc_ios, dc_merges, dc_sec, fl_ios; - unsigned int major, minor; - int i; - - i =3D sscanf(line, "%u %u %s %lu %lu %lu" - "%lu %lu %lu %lu %u %u %u %u" - "%lu %lu %lu %u %lu %u", - &major, &minor, dev_name, - &rd_ios, &rd_merges_or_rd_sec, &rd_sec_or_wr_ios, - &rd_ticks_or_wr_sec, &wr_ios, &wr_merges, &wr_sec, - &wr_ticks, &ios_pgr, &tot_ticks, &rq_ticks, - &dc_ios, &dc_merges, &dc_sec, &dc_ticks, - &fl_ios, &fl_ticks); - - if (i < 7) { - continue; - } - - diskstatinfo =3D g_new0(GuestDiskStatsInfo, 1); - diskstatinfo->name =3D g_strdup(dev_name); - diskstatinfo->major =3D major; - diskstatinfo->minor =3D minor; - - diskstat =3D g_new0(GuestDiskStats, 1); - if (i =3D=3D 7) { - diskstat->has_read_ios =3D true; - diskstat->read_ios =3D rd_ios; - diskstat->has_read_sectors =3D true; - diskstat->read_sectors =3D rd_merges_or_rd_sec; - diskstat->has_write_ios =3D true; - diskstat->write_ios =3D rd_sec_or_wr_ios; - diskstat->has_write_sectors =3D true; - diskstat->write_sectors =3D rd_ticks_or_wr_sec; - } - if (i >=3D 14) { - diskstat->has_read_ios =3D true; - diskstat->read_ios =3D rd_ios; - diskstat->has_read_sectors =3D true; - diskstat->read_sectors =3D rd_sec_or_wr_ios; - diskstat->has_read_merges =3D true; - diskstat->read_merges =3D rd_merges_or_rd_sec; - diskstat->has_read_ticks =3D true; - diskstat->read_ticks =3D rd_ticks_or_wr_sec; - diskstat->has_write_ios =3D true; - diskstat->write_ios =3D wr_ios; - diskstat->has_write_sectors =3D true; - diskstat->write_sectors =3D wr_sec; - diskstat->has_write_merges =3D true; - diskstat->write_merges =3D wr_merges; - diskstat->has_write_ticks =3D true; - diskstat->write_ticks =3D wr_ticks; - diskstat->has_ios_pgr =3D true; - diskstat->ios_pgr =3D ios_pgr; - diskstat->has_total_ticks =3D true; - diskstat->total_ticks =3D tot_ticks; - diskstat->has_weight_ticks =3D true; - diskstat->weight_ticks =3D rq_ticks; - } - if (i >=3D 18) { - diskstat->has_discard_ios =3D true; - diskstat->discard_ios =3D dc_ios; - diskstat->has_discard_merges =3D true; - diskstat->discard_merges =3D dc_merges; - diskstat->has_discard_sectors =3D true; - diskstat->discard_sectors =3D dc_sec; - diskstat->has_discard_ticks =3D true; - diskstat->discard_ticks =3D dc_ticks; - } - if (i >=3D 20) { - diskstat->has_flush_ios =3D true; - diskstat->flush_ios =3D fl_ios; - diskstat->has_flush_ticks =3D true; - diskstat->flush_ticks =3D fl_ticks; - } - - diskstatinfo->stats =3D g_steal_pointer(&diskstat); - QAPI_LIST_APPEND(tail, diskstatinfo); - diskstatinfo =3D NULL; - } - free(line); - fclose(fp); - return head; -#else - g_debug("disk stats reporting available only for Linux"); - return NULL; -#endif -} - -GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp) -{ - return guest_get_diskstats(errp); -} - -GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) -{ - GuestCpuStatsList *head =3D NULL, **tail =3D &head; - const char *cpustats =3D "/proc/stat"; - int clk_tck =3D sysconf(_SC_CLK_TCK); - FILE *fp; - size_t n; - char *line =3D NULL; - - fp =3D fopen(cpustats, "r"); - if (fp =3D=3D NULL) { - error_setg_errno(errp, errno, "open(\"%s\")", cpustats); - return NULL; - } - - while (getline(&line, &n, fp) !=3D -1) { - GuestCpuStats *cpustat =3D NULL; - GuestLinuxCpuStats *linuxcpustat; - int i; - unsigned long user, system, idle, iowait, irq, softirq, steal, gue= st; - unsigned long nice, guest_nice; - char name[64]; - - i =3D sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", - name, &user, &nice, &system, &idle, &iowait, &irq, &sof= tirq, - &steal, &guest, &guest_nice); - - /* drop "cpu 1 2 3 ...", get "cpuX 1 2 3 ..." only */ - if ((i =3D=3D EOF) || strncmp(name, "cpu", 3) || (name[3] =3D=3D '= \0')) { - continue; - } - - if (i < 5) { - slog("Parsing cpu stat from %s failed, see \"man proc\"", cpus= tats); - break; - } - - cpustat =3D g_new0(GuestCpuStats, 1); - cpustat->type =3D GUEST_CPU_STATS_TYPE_LINUX; - - linuxcpustat =3D &cpustat->u.q_linux; - linuxcpustat->cpu =3D atoi(&name[3]); - linuxcpustat->user =3D user * 1000 / clk_tck; - linuxcpustat->nice =3D nice * 1000 / clk_tck; - linuxcpustat->system =3D system * 1000 / clk_tck; - linuxcpustat->idle =3D idle * 1000 / clk_tck; - - if (i > 5) { - linuxcpustat->has_iowait =3D true; - linuxcpustat->iowait =3D iowait * 1000 / clk_tck; - } - - if (i > 6) { - linuxcpustat->has_irq =3D true; - linuxcpustat->irq =3D irq * 1000 / clk_tck; - linuxcpustat->has_softirq =3D true; - linuxcpustat->softirq =3D softirq * 1000 / clk_tck; - } - - if (i > 8) { - linuxcpustat->has_steal =3D true; - linuxcpustat->steal =3D steal * 1000 / clk_tck; - } - - if (i > 9) { - linuxcpustat->has_guest =3D true; - linuxcpustat->guest =3D guest * 1000 / clk_tck; - } - - if (i > 10) { - linuxcpustat->has_guest =3D true; - linuxcpustat->guest =3D guest * 1000 / clk_tck; - linuxcpustat->has_guestnice =3D true; - linuxcpustat->guestnice =3D guest_nice * 1000 / clk_tck; - } - - QAPI_LIST_APPEND(tail, cpustat); - } - - free(line); - fclose(fp); - return head; -} =20 #else /* defined(__linux__) */ =20 --=20 2.45.1