Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbpf-tools/profile: Check if nr_cpus is zero #5120

Closed
wants to merge 1 commit into from

Conversation

Bojun-Seo
Copy link
Contributor

No description provided.

@@ -584,7 +584,7 @@ int main(int argc, char **argv)
libbpf_set_print(libbpf_print_fn);

nr_cpus = libbpf_num_possible_cpus();
if (nr_cpus < 0) {
if (nr_cpus <= 0) {
printf("failed to get # of possible cpus: '%s'!\n",
strerror(-nr_cpus));
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with you that a result of 0 from libbpf_num_possible_cpus can be treated as a failure case.

However, on line 589, strerror(0) will print 'Success'. Please check this. Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you. So I changed the code. I hope you to review it again.
Thanks for the review.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about using just one if statement for the failure case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the code to use one if statement for the failure case.

@@ -584,9 +584,9 @@ int main(int argc, char **argv)
libbpf_set_print(libbpf_print_fn);

nr_cpus = libbpf_num_possible_cpus();
if (nr_cpus < 0) {
if (nr_cpus <= 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The current code is correct. libbpf_num_possible_cpus() never returns 0.
In libbpf, we have

int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
{
        int fd, err = 0, len;
        char buf[128];

        fd = open(fcpu, O_RDONLY | O_CLOEXEC);
        if (fd < 0) {
                err = -errno;
                pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
                return err;
        }
        len = read(fd, buf, sizeof(buf));
        close(fd);      
        if (len <= 0) { 
                err = len ? -errno : -EINVAL;
                pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
                return err;
        }
        if (len >= sizeof(buf)) {
                pr_warn("CPU mask is too big in file %s\n", fcpu);
                return -E2BIG;
        }
        buf[len] = '\0';
                
        return parse_cpu_mask_str(buf, mask, mask_sz);
}

if there is no cpu, negative value (err number) will be return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think adding this kind of error handling code can make the program more robust and less dependent on other modules. But as you said, since libbpf_num_possible_cpus never returns zero, I'll close this PR.

@Bojun-Seo Bojun-Seo closed this Oct 21, 2024
@Bojun-Seo Bojun-Seo deleted the profile2 branch November 25, 2024 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants