Skip to content

Use ?: to chain comparison functions (for now, in lldp only) - #11140

Merged
poettering merged 2 commits into
systemd:masterfrom
filbranden:lldpcmp1
Dec 15, 2018
Merged

poettering merged 2 commits into
systemd:masterfrom
filbranden:lldpcmp1

Conversation

@filbranden

Copy link
Copy Markdown
Contributor

This is in some ways a follow up to #9789 and part of the discussion we had there.

It turns out the ?: operator in C does just what we want. It keeps the result if non-zero, otherwise keeps evaluating the next calls on the chain.

I started with this simple PR that just uses it in one place, but if this looks acceptable, I'd like to refactor more of the compare_func's to use this.

Another idea is to simplify memcmp() + a CMP() for the lengths into a single macro that takes two pointers and two lengths (not sure about the name... BINCMP? BSTRCMP? BUFCMP? MEMCMP)

Also, one for booleans BOOLCMP() would make sense, to convert code like this:

        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                return -1;
        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                return 1;

into:

      return BOOLCMP(x->enabled != SD_EVENT_OFF, y->enabled != SD_EVENT_OFF)
          ?: CMP(x->priority, y->priority)
          ?: CMP(x->pending_iteration, y->pending_iteration);

(I'm not 100% whether != or == is better here, but you get the idea...)

Anyways, starting with a simple one to get the ball rolling, we can get the next ones done later.

Cheers,
Filipe

@poettering

Copy link
Copy Markdown
Member

hmm, interesting. not a fan of using downgrade-to-bool for numerals, but maybe it's OK in this case. @keszybz @yuwata opinion?

@poettering

Copy link
Copy Markdown
Member

Another idea is to simplify memcmp() + a CMP() for the lengths into a single macro that takes two pointers and two lengths (not sure about the name... BINCMP? BSTRCMP? BUFCMP? MEMCMP)

why a macro? could be a regular function. memcmp_shorter_wins() or so? sounds useful to have. I like

@poettering

Copy link
Copy Markdown
Member

Also, one for booleans BOOLCMP() would make sense, to convert code like this:

hmm, actually plain CMP() can cover this usecase just fine, too, no?

@filbranden

Copy link
Copy Markdown
Contributor Author

Trying to figure out why the test case has a memleak in it...

Direct leak of 200 byte(s) in 1 object(s) allocated from:
    #0 0x7f0ff0e73a50 in __interceptor_calloc (/lib64/libasan.so.5+0xefa50)
    #1 0x55f47ac4107e in lldp_neighbor_new ../src/libsystemd-network/lldp-neighbor.c:100
    #2 0x55f47ac4ac37 in lldp_receive_datagram ../src/libsystemd-network/sd-lldp.c:208
    #3 0x7f0ff02dd8eb in source_dispatch ../src/libsystemd/sd-event/sd-event.c:2816
    #4 0x7f0ff02e3d26 in sd_event_dispatch ../src/libsystemd/sd-event/sd-event.c:3229
    #5 0x7f0ff02e4c6e in sd_event_run ../src/libsystemd/sd-event/sd-event.c:3286
    #6 0x55f47ac3de37 in test_multiple_neighbors_sorted ../src/libsystemd-network/test-lldp.c:322
    #7 0x55f47ac3f0db in main ../src/libsystemd-network/test-lldp.c:371
    #8 0x7f0feea96412 in __libc_start_main (/lib64/libc.so.6+0x24412)

Any ideas? I was staring at the code but still can't see it...

@yuwata

yuwata commented Dec 14, 2018

Copy link
Copy Markdown
Member

Another idea is to simplify memcmp() + a CMP() for the lengths into a single macro that takes two pointers and two lengths (not sure about the name... BINCMP? BSTRCMP? BUFCMP? MEMCMP)

why a macro? could be a regular function. memcmp_shorter_wins() or so? sounds useful to have. I like

+1

Also, one for booleans BOOLCMP() would make sense, to convert code like this:

hmm, actually plain CMP() can cover this usecase just fine, too, no?

How about the following?

CMP(x->enabled == SD_EVENT_OFF, y->enabled == SD_EVENT_OFF)

@filbranden

Copy link
Copy Markdown
Contributor Author

My idea was:

#define BOOLCMP(x, y) CMP(!!(x), !!(y))

Which should be a bit safer if by any chance it's passed integers-as-booleans... Also, to the reader, it's clear that a comparison of booleans was actually needed.

Yes, function for memcmp() wrapper is a good idea, just not in love with the name memcmp_shorter_wins() :-) Any better suggestions?

@yuwata

yuwata commented Dec 14, 2018

Copy link
Copy Markdown
Member

Trying to figure out why the test case has a memleak in it...

Direct leak of 200 byte(s) in 1 object(s) allocated from:
    #0 0x7f0ff0e73a50 in __interceptor_calloc (/lib64/libasan.so.5+0xefa50)
    #1 0x55f47ac4107e in lldp_neighbor_new ../src/libsystemd-network/lldp-neighbor.c:100
    #2 0x55f47ac4ac37 in lldp_receive_datagram ../src/libsystemd-network/sd-lldp.c:208
    #3 0x7f0ff02dd8eb in source_dispatch ../src/libsystemd/sd-event/sd-event.c:2816
    #4 0x7f0ff02e3d26 in sd_event_dispatch ../src/libsystemd/sd-event/sd-event.c:3229
    #5 0x7f0ff02e4c6e in sd_event_run ../src/libsystemd/sd-event/sd-event.c:3286
    #6 0x55f47ac3de37 in test_multiple_neighbors_sorted ../src/libsystemd-network/test-lldp.c:322
    #7 0x55f47ac3f0db in main ../src/libsystemd-network/test-lldp.c:371
    #8 0x7f0feea96412 in __libc_start_main (/lib64/libc.so.6+0x24412)

Any ideas? I was staring at the code but still can't see it...

lldp_add_neighbor() increments reference counter for old, but it seems not unref()ed...

@filbranden

Copy link
Copy Markdown
Contributor Author

lldp_add_neighbor() increments reference counter for old, but it seems not unref()ed...

No, I don't think that's it: _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *old = NULL;.

I think a neighbor may end up not being actually free'd if it's not lldp_neighbor_unlink()'d though, so maybe there's a problem there? Really not sure where the issue is...

@filbranden

Copy link
Copy Markdown
Contributor Author

I ended up going with memcmp_nn() since I found a precedent for it in ascii_strcasecmp_nn().

The memory leak is probably still there... Not really sure what's going on there. Running this test under valgrind repeatedly shows a different LLDP neighbor (or sometimes more than one) being leaked each time, so maybe there's a race condition somewhere?

@poettering poettering left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. One minor nitpick though.

Comment thread src/basic/util.h Resolved
Comment thread src/basic/util.h Resolved
@poettering poettering added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks util-lib tests lldp labels Dec 14, 2018
…ghbors

In particular, check that the order of the results is consistent.

This test coverage will be useful in order to refactor the compare_func
used while sorting the results.

Tested: ninja -C build/ test
The ?: operator is very useful for chaining comparison functions
(strcmp, memcmp, CMP), since its behavior is to return the result
of the comparison function call if non-zero, or continue evaluating
the chain of comparison functions.

This simplifies the code in that using a temporary `r` variable
to store the function results is no longer necessary and the checks
for non-zero to return are no longer needed either, resulting in a
typical three-fold reduction to the number of lines in the code.

Introduce a new memcmp_nn() to compare two memory buffers in
lexicographic order, taking length in consideration.

Tested: $ ninja -C build/ test

All test cases pass. In particular, test_multiple_neighbors_sorted()
in test-lldp would catch regressions introduced by this commit.
@filbranden

Copy link
Copy Markdown
Contributor Author

Still looking at the memory leak... It seems there's a bug somewhere in hashmap implementation, at least I can see that internal_hashmap_clear() is not really going through the 6 items, but only 4 or 5 of them... Will keep digging.

@filbranden

Copy link
Copy Markdown
Contributor Author

Ok I figured out the memory leak... The problem is that hashmap_clear() will call the destructor on each value, the destructor for these LLDP entries is lldp_neighbor_unlink() which itself will call hashmap_remove() and move things around in the hashmap, therefore leaking some objects...

I'll update the destructor to use a function that does all but call hashmap_remove(), I guess that should fix the leak. I'll send a separate PR for that.

@filbranden

Copy link
Copy Markdown
Contributor Author

Fix for the memory leak in #11163. I'll come back to this one (keeping just the memcmp_nn part) after that one is in.

@poettering
poettering merged commit ffd7916 into systemd:master Dec 15, 2018
@evverx

evverx commented Dec 15, 2018

Copy link
Copy Markdown
Contributor

@poettering I'm wondering why you keep merging PRs that break the CI.

@filbranden

Copy link
Copy Markdown
Contributor Author

My bad, I should have marked this as "do-not-merge" until #11163 was resolved... Let's work on that one to fix the memory leak.

@evverx

evverx commented Dec 15, 2018

Copy link
Copy Markdown
Contributor

I'm sorry. I didn't mean to be grumpy. Actually I'm glad you added the test that uncovered the memory leak. Thank you!

@filbranden

Copy link
Copy Markdown
Contributor Author

@evverx You do have a point though!

In this case, this test being in means all further commits would break for that same reason.

I just filed #11172 to revert the test case until the memory leak is fixed (keeping the ?: part in is fine, since that was working fine and the test verified that.)

I think part of the problem is sometimes it's hard to tell real failures from flakes... Maybe something we need to address. I'll open a separate issue about that. Thanks!

@filbranden
filbranden deleted the lldpcmp1 branch December 15, 2018 17:49
@evverx

evverx commented Dec 15, 2018

Copy link
Copy Markdown
Contributor

@filbranden thank you! I've just restarted all the jobs that failed manually. https://travis-ci.org/systemd/systemd/pull_requests should become less noisy soon.

I think part of the problem is sometimes it's hard to tell real failures from flakes...

That's true but it's usually applicable to Ubuntu CI (which seems to be abandoned). Travis CI and CentOS CI are more or less maintained actively (that is the tests that are flaky are skipped there if they can't be fixed for some reason: systemd/systemd-centos-ci#28, failed jobs are restarted and so on).

@evverx

evverx commented Dec 15, 2018 •

Copy link
Copy Markdown
Contributor

Interestingly, the refs/pull/*/merge branches haven't been updated yet so the test failed even after I restarted the jobs manually. I'm wondering how long it will take for GitHub to pick up fc83352.

@keszybz keszybz removed the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Dec 17, 2018
@keszybz

keszybz commented Dec 17, 2018

Copy link
Copy Markdown
Member

hmm, interesting. not a fan of using downgrade-to-bool for numerals, but maybe it's OK in this case. @keszybz @yuwata opinion?

I think it's worth it. Once we get accustomed to the new pattern, its brevity will be more important than the underlying implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants