Commit 0ea6d55
perf(userspace/libscap): rewrite
The previous logic used 1 single `fopen()` to open the
`/proc/<pid>/fdinfo/<fd>` file, and 1 `fgets()` for each single line
in the file; then, each line was matched against a set of prefixes
using `strncmp()` and, for each match, `strtoul()` was used to parse
the numeric content.
The new parsing logic introduces the following optimizations:
- use `open()` and `read_exact()` to avoid `fopen()` and `fgets()`
allocation, buffering and lock acquisition overheads
- try to read the entire file content with a single `read()`, reducing
the overhead of issuing ~3 `fgets()` (3 is the position of the last
interesting row in the `/proc/<pid>/fdinfo/<fd>` file, which is the
`mnt_id: ...` line for the current logic). The needed information
are present in the first few lines (within 512 bytes), so no new
`read()` are issued if the file content is bigger
- do line prefix-matching using a switch lookup table, to avoid
wasting cycles on unneeded comparisons
Finally, replace `strncmp()` and `strtoul()` usage with calls to
`MEMCMP_LITERAL()` and `str_parse_u64()`.
Signed-off-by: Leonardo Di Giovanna <[email protected]>/proc/pid/fdinfo/fd parsing logic1 parent ba1619a commit 0ea6d55
1 file changed
Lines changed: 66 additions & 33 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| |||
336 | 338 | | |
337 | 339 | | |
338 | 340 | | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
346 | 345 | | |
347 | 346 | | |
| 347 | + | |
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
369 | 360 | | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
374 | 367 | | |
375 | | - | |
376 | | - | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
377 | 403 | | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
378 | 407 | | |
379 | | - | |
380 | 408 | | |
381 | | - | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
382 | 415 | | |
383 | 416 | | |
384 | 417 | | |
| |||
0 commit comments