diff options
author | Yabin Cui <yabinc@google.com> | 2015-09-29 16:11:45 -0700 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2015-09-29 17:49:37 -0700 |
commit | cb6f599c44b144db26c13507dbd89805b8fe03be (patch) | |
tree | 2cd3dfa6f2d8b35b7c7db91a1b5c1a21a3875901 /libc/private/get_cpu_count_from_string.h | |
parent | f16f4f8a6bac035db5f25538ddb4fe810d0719db (diff) |
Fix the way to count online cpus.
Read /proc/stat to count online cpus is not correct for all android
kernels. Change to reading /sys/devices/system/cpu/online instead.
Bug: 24376925
Change-Id: I3785a6c7aa15a467022a9a261b457194d688fb38
Diffstat (limited to 'libc/private/get_cpu_count_from_string.h')
-rw-r--r-- | libc/private/get_cpu_count_from_string.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/libc/private/get_cpu_count_from_string.h b/libc/private/get_cpu_count_from_string.h new file mode 100644 index 000000000..a0cb95de0 --- /dev/null +++ b/libc/private/get_cpu_count_from_string.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <ctype.h> +#include <stdlib.h> + +// Parse a string like: 0, 2-4, 6. +static int GetCpuCountFromString(const char* s) { + int cpu_count = 0; + int last_cpu = -1; + while (*s != '\0') { + if (isdigit(*s)) { + int cpu = static_cast<int>(strtol(s, const_cast<char**>(&s), 10)); + if (last_cpu != -1) { + cpu_count += cpu - last_cpu; + } else { + cpu_count++; + } + last_cpu = cpu; + } else { + if (*s == ',') { + last_cpu = -1; + } + s++; + } + } + return cpu_count; +} |