static unsigned long virNodeCountThreadSiblings(const char *dir, unsigned int cpu) { unsigned long ret = 0; char *path; FILE *pathfp; char str[1024]; size_t i; if (virAsprintf(&path, "%s/cpu%u/topology/thread_siblings", dir, cpu) < 0) return 0; pathfp = fopen(path, "r"); if (pathfp == NULL) { /* If file doesn't exist, then pretend our only * sibling is ourself */ if (errno == ENOENT) { VIR_FREE(path); return 1; } virReportSystemError(errno, _("cannot open %s"), path); VIR_FREE(path); return 0; } if (fgets(str, sizeof(str), pathfp) == NULL) { virReportSystemError(errno, _("cannot read from %s"), path); goto cleanup; } i = 0; while (str[i] != '\0') { if (c_isdigit(str[i])) ret += count_one_bits(str[i] - '0'); else if (str[i] >= 'A' && str[i] <= 'F') ret += count_one_bits(str[i] - 'A' + 10); else if (str[i] >= 'a' && str[i] <= 'f') ret += count_one_bits(str[i] - 'a' + 10); i++; } cleanup: VIR_FORCE_FCLOSE(pathfp); VIR_FREE(path); return ret; }
static unsigned long virHostCPUCountThreadSiblings(const char *dir, unsigned int cpu) { unsigned long ret = 0; char *path; char *str = NULL; size_t i; if (virAsprintf(&path, "%s/cpu%u/topology/thread_siblings", dir, cpu) < 0) return 0; if (!virFileExists(path)) { /* If file doesn't exist, then pretend our only * sibling is ourself */ ret = 1; goto cleanup; } if (virFileReadAll(path, SYSFS_THREAD_SIBLINGS_LIST_LENGTH_MAX, &str) < 0) goto cleanup; for (i = 0; str[i] != '\0'; i++) { if (c_isxdigit(str[i])) ret += count_one_bits(virHexToBin(str[i])); } cleanup: VIR_FREE(str); VIR_FREE(path); return ret; }
/*********************************************************** * Function: // main * Description: // 主函数 ***********************************************************/ int main( void ) { unsigned int v = 0xAA; printf( "0x%X one bits count is:%d. \n", v, count_one_bits( v ) ); return 0; }
size_t traverse(cow_trie_p map) { size_t count = 0; count += map->size; for (int i=0;i<count_one_bits(map->child_bitmap);i++) { count += traverse(cow_trie_children(map)[i]); } return count; }
static unsigned long count_thread_siblings(unsigned int cpu) { unsigned long ret = 0; char *path; FILE *pathfp; char str[1024]; int i; if (virAsprintf(&path, "%s/cpu%u/topology/thread_siblings", sysfs_path, cpu) < 0) { virReportOOMError(); return 0; } pathfp = fopen(path, "r"); if (pathfp == NULL) { virReportSystemError(errno, _("cannot open %s"), path); VIR_FREE(path); return 0; } if (fgets(str, sizeof(str), pathfp) == NULL) { virReportSystemError(errno, _("cannot read from %s"), path); goto cleanup; } i = 0; while (str[i] != '\0') { if (c_isdigit(str[i])) ret += count_one_bits(str[i] - '0'); else if (str[i] >= 'A' && str[i] <= 'F') ret += count_one_bits(str[i] - 'A' + 10); else if (str[i] >= 'a' && str[i] <= 'f') ret += count_one_bits(str[i] - 'a' + 10); i++; } cleanup: VIR_FORCE_FCLOSE(pathfp); VIR_FREE(path); return ret; }
void channel_mask_splits(struct i_array *counts, unsigned int channel_count, unsigned int channel_mask) { /*Although the WAVEFORMATEXTENSIBLE channel mask supports more left/right channels than these, everything beyond side-left/side-right is stored with a center channel in-between which means we can't pull them apart in pairs.*/ unsigned int masks[] = {0x3, 0x1, 0x2, /*fLfR, fL, fR*/ 0x4, 0x8, /*fC, LFE*/ 0x30, 0x10, 0x20, /*bLbR, bL, bR*/ 0xC0, 0x40, 0x80, /*fLoC, fRoC, fLoC, fRoC*/ 0x100, /*bC*/ 0x600, 0x200, 0x400, /*sLsR, sL, sR*/ 0}; unsigned int channels; int i; assert(channel_count > 0); /*first, try to pull left/right channels out of the mask*/ for (i = 0; channel_mask && masks[i]; i++) { if (channel_mask & masks[i]) { channels = count_one_bits(masks[i]); ia_append(counts, channels); channel_count -= channels; channel_mask ^= masks[i]; } } /*any leftover channels are sent out in separate blocks (which may happen with a mask of 0)*/ for (; channel_count > 0; channel_count--) { ia_append(counts, 1); } }