Browse Source

Cppcheck fixes (#6386)

* collector: cgroups: Fix a cppcheck warning

Cppcheck was throwing the following warning in
collectors/cgroups.plugin/cgroup-network.c

[collectors/cgroups.plugin/cgroup-network.c:233]: (warning) Assignment of function parameter has no effect outside the function. Did you forget dereferencing it?

One of the arguments to switch_namespace() is 'const char *prefix', in
this function we were checking if prefix was NULL and if so setting it
(local scope wise) to "".

While this wasn't technically incorrect in this context. It is also
unnecessary as the prefix variable is only passed to the proc_pid_fd()
function where the same check happens, so we can simply remove the
offending line.

Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net>

* tests/profile: Remove somewhat redundant code

cppcheck was throwing a warning in benchmark-line-parsing.c

[tests/profile/benchmark-line-parsing.c:648]: (warning) Unnecessary
comparison of static strings, this comes from

    (void)strcmp("1", "2");

in main()

That is amongst a group of three function calls preceded by the comment

    // cache functions

But then test1() which uses strcmp() is called twice anyway with the
timing result of just the second one used, so the dummy strcmp() call
would seem superfluous.

I would say the same is true for the call to strtoull()

    (void)strtoull("123", NULL, 0);

as that is also used in test1().

I actually ran this benchmark with and without the calls to all three
functions, i.e

    // cache functions
    (void)simple_hash2("hello world");
    (void)strcmp("1", "2");
    (void)strtoull("123", NULL, 0);

With the above functions being called

test1() average time = 7801604
test2() average time = 1333162

Without those three function calls

test1() average time = 7779905
test2() average time = 1321438

Those are the averages of three runs. test1() uses strcmp() & strtoull()
and test2() uses simple_hash2(), so in that run, not calling the three
functions initially was actually quicker.

Subsequent runs of each show similar numbers with each edging the other
out, however the difference is in the noise.

Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net>
Andrew Clayton 5 years ago
parent
commit
d6ba820be6

+ 0 - 1
collectors/cgroups.plugin/cgroup-network.c

@@ -230,7 +230,6 @@ static struct ns {
 };
 
 int switch_namespace(const char *prefix, pid_t pid) {
-    if(!prefix) prefix = "";
 
 #ifdef HAVE_SETNS
 

+ 0 - 5
tests/profile/benchmark-line-parsing.c

@@ -643,11 +643,6 @@ void main(void)
     total_active_file_hash = simple_hash("total_active_file");
     total_unevictable_hash = simple_hash("total_unevictable");
 
-    // cache functions
-    (void)simple_hash2("hello world");
-    (void)strcmp("1", "2");
-    (void)strtoull("123", NULL, 0);
-
   unsigned long i, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7;
   unsigned long max = 1000000;