From: Lucas De Marchi <lucas.demarchi@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Tvrtko Ursulin <tursulin@ursulin.net>,
Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>,
Lucas De Marchi <lucas.demarchi@intel.com>
Subject: [PATCH i-g-t 6/8] lib/igt_drm_fdinfo: Stop passing key twice
Date: Tue, 2 Apr 2024 15:17:14 -0700 [thread overview]
Message-ID: <20240402221716.1840148-7-lucas.demarchi@intel.com> (raw)
In-Reply-To: <20240402221716.1840148-1-lucas.demarchi@intel.com>
Change strstartswith() so it also returns the length, which then can be
used inside the branch when it matches. A good compiler shall optimize
out the strlen call since the argument is a constant literal.
With this, the find_kv() is not needed anymore and the difference with
regard the other branches can be summarized with a new ignore_space()
helper and the fact it matches the entire key by appending the ':'. The
helper is added on top of the file so it can be reused later.
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
---
lib/igt_drm_fdinfo.c | 84 +++++++++++++++++++++++++-------------------
1 file changed, 48 insertions(+), 36 deletions(-)
diff --git a/lib/igt_drm_fdinfo.c b/lib/igt_drm_fdinfo.c
index 15f71e172..a0b41e956 100644
--- a/lib/igt_drm_fdinfo.c
+++ b/lib/igt_drm_fdinfo.c
@@ -53,6 +53,14 @@ static size_t read_fdinfo(char *buf, const size_t sz, int at, const char *name)
return count > 0 ? count : 0;
}
+static const char *ignore_space(const char *s)
+{
+ for (; *s && isspace(*s); s++)
+ ;
+
+ return s;
+}
+
static int parse_engine(char *line, struct drm_client_fdinfo *info,
size_t prefix_len,
const char **name_map, unsigned int map_entries,
@@ -104,21 +112,12 @@ static int parse_engine(char *line, struct drm_client_fdinfo *info,
return found;
}
-static const char *find_kv(const char *buf, const char *key, size_t keylen)
+static const char *ignore_space(const char *s)
{
- const char *p;
-
- if (strncmp(buf, key, keylen))
- return NULL;
-
- p = buf + keylen;
- if (*p != ':')
- return NULL;
-
- for (p++; *p && isspace(*p); p++)
+ for (; *s && isspace(*s); s++)
;
- return *p ? p : NULL;
+ return s;
}
static int parse_region(char *line, struct drm_client_fdinfo *info,
@@ -205,6 +204,11 @@ out:
} \
} while (0)
+#define strstartswith(a, b, plen__) ({ \
+ *plen__ = strlen(b); \
+ strncmp(a, b, *plen__) == 0; \
+})
+
unsigned int
__igt_parse_drm_fdinfo(int dir, const char *fd, struct drm_client_fdinfo *info,
const char **name_map, unsigned int map_entries,
@@ -222,30 +226,39 @@ __igt_parse_drm_fdinfo(int dir, const char *fd, struct drm_client_fdinfo *info,
while ((l = strtok_r(_buf, "\n", &ctx))) {
uint64_t val = 0;
+ size_t keylen;
const char *v;
int idx;
_buf = NULL;
- if ((v = find_kv(l, "drm-driver", strlen("drm-driver")))) {
- strncpy(info->driver, v, sizeof(info->driver) - 1);
- good++;
- } else if ((v = find_kv(l, "drm-client-id", strlen("drm-client-id")))) {
- info->id = atol(v);
- good++;
- } else if ((v = find_kv(l, "drm-pdev", strlen("drm-pdev")))) {
- /* optional */
+ if (strstartswith(l, "drm-driver:", &keylen)) {
+ v = l + keylen;
+ v = ignore_space(v);
+ if (*v) {
+ strncpy(info->driver, v, sizeof(info->driver) - 1);
+ good++;
+ }
+ } else if (strstartswith(l, "drm-client-id:", &keylen)) {
+ v = l + keylen;
+ v = ignore_space(v);
+ if (*v) {
+ info->id = atol(v);
+ good++;
+ }
+ } else if (strstartswith(l, "drm-pdev:", &keylen)) {
+ v = l + keylen;
+ v = ignore_space(v);
strncpy(info->pdev, v, sizeof(info->pdev) - 1);
- } else if (!strncmp(l, "drm-engine-capacity-", 20)) {
- idx = parse_engine(l, info,
- strlen("drm-engine-capacity-"),
+ } else if (strstartswith(l, "drm-engine-capacity-", &keylen)) {
+ idx = parse_engine(l, info, keylen,
name_map, map_entries, &val);
if (idx >= 0) {
info->capacity[idx] = val;
num_capacity++;
}
- } else if (!strncmp(l, "drm-engine-", 11)) {
- idx = parse_engine(l, info, strlen("drm-engine-"),
+ } else if (strstartswith(l, "drm-engine-", &keylen)) {
+ idx = parse_engine(l, info, keylen,
name_map, map_entries, &val);
if (idx >= 0) {
if (!info->capacity[idx])
@@ -255,25 +268,24 @@ __igt_parse_drm_fdinfo(int dir, const char *fd, struct drm_client_fdinfo *info,
if (idx > info->last_engine_index)
info->last_engine_index = idx;
}
- } else if (!strncmp(l, "drm-total-", 10)) {
- idx = parse_region(l, info, strlen("drm-total-"),
+ } else if (strstartswith(l, "drm-total-", &keylen)) {
+ idx = parse_region(l, info, keylen,
region_map, region_entries, &val);
UPDATE_REGION(idx, total, val);
- } else if (!strncmp(l, "drm-shared-", 11)) {
- idx = parse_region(l, info, strlen("drm-shared-"),
+ } else if (strstartswith(l, "drm-shared-", &keylen)) {
+ idx = parse_region(l, info, keylen,
region_map, region_entries, &val);
UPDATE_REGION(idx, shared, val);
-
- } else if (!strncmp(l, "drm-resident-", 13)) {
- idx = parse_region(l, info, strlen("drm-resident-"),
+ } else if (strstartswith(l, "drm-resident-", &keylen)) {
+ idx = parse_region(l, info, keylen,
region_map, region_entries, &val);
UPDATE_REGION(idx, resident, val);
- } else if (!strncmp(l, "drm-purgeable-", 14)) {
- idx = parse_region(l, info, strlen("drm-purgeable-"),
+ } else if (strstartswith(l, "drm-purgeable-", &keylen)) {
+ idx = parse_region(l, info, keylen,
region_map, region_entries, &val);
UPDATE_REGION(idx, purgeable, val);
- } else if (!strncmp(l, "drm-active-", 11)) {
- idx = parse_region(l, info, strlen("drm-active-"),
+ } else if (strstartswith(l, "drm-active-", &keylen)) {
+ idx = parse_region(l, info, keylen,
region_map, region_entries, &val);
UPDATE_REGION(idx, active, val);
}
--
2.43.0
next prev parent reply other threads:[~2024-04-02 22:17 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-02 22:17 [PATCH i-g-t 0/8] Refactors and fixes for drm_clients Lucas De Marchi
2024-04-02 22:17 ` [PATCH i-g-t 1/8] lib/igt_drm_clients: Use calloc Lucas De Marchi
2024-04-03 15:27 ` Umesh Nerlige Ramappa
2024-04-03 17:23 ` Kamil Konieczny
2024-04-02 22:17 ` [PATCH i-g-t 2/8] lib/igt_drm_clients: Fix sizeof calculation Lucas De Marchi
2024-04-03 15:28 ` Umesh Nerlige Ramappa
2024-04-03 17:25 ` Kamil Konieczny
2024-04-02 22:17 ` [PATCH i-g-t 3/8] lib/igt_drm_clients: Fix leaks Lucas De Marchi
2024-04-03 15:39 ` Umesh Nerlige Ramappa
2024-04-03 16:09 ` Tvrtko Ursulin
2024-04-03 17:26 ` Kamil Konieczny
2024-04-02 22:17 ` [PATCH i-g-t 4/8] gputop: Free clients on exit Lucas De Marchi
2024-04-03 16:03 ` Umesh Nerlige Ramappa
2024-04-03 16:13 ` Tvrtko Ursulin
2024-04-03 17:28 ` Kamil Konieczny
2024-04-02 22:17 ` [PATCH i-g-t 5/8] lib/igt_drm_fdinfo: Simplify find_kv() Lucas De Marchi
2024-04-03 17:48 ` Kamil Konieczny
2024-04-02 22:17 ` Lucas De Marchi [this message]
2024-04-03 17:56 ` [PATCH i-g-t 6/8] lib/igt_drm_fdinfo: Stop passing key twice Kamil Konieczny
2024-04-02 22:17 ` [PATCH i-g-t 7/8] lib/igt_drm_fdinfo: Remove prefix arg from parse functions Lucas De Marchi
2024-04-03 23:53 ` Umesh Nerlige Ramappa
2024-04-02 22:17 ` [PATCH i-g-t 8/8] lib/igt_drm_clients: lazy stat process Lucas De Marchi
2024-04-03 16:24 ` Tvrtko Ursulin
2024-04-02 23:59 ` ✗ Fi.CI.BAT: failure for Refactors and fixes for drm_clients Patchwork
2024-04-03 0:31 ` ✓ CI.xeBAT: success " Patchwork
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240402221716.1840148-7-lucas.demarchi@intel.com \
--to=lucas.demarchi@intel.com \
--cc=igt-dev@lists.freedesktop.org \
--cc=tursulin@ursulin.net \
--cc=umesh.nerlige.ramappa@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox