Exemplo n.º 1
0
INTVAL
Parrot_get_num_cpus(SHIM_INTERP) {
    INTVAL nprocs = -1;

#ifdef _SC_NPROCESSORS_ONLN
    nprocs = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(PARROT_HAS_HEADER_LIBCPUID)
    struct cpu_raw_data_t raw;
    struct cpu_id_t data;

    if (!cpuid_present()) {
        printf("cpuid_present failed\n");
        exit(EXIT_FAILURE);
    }
    if (cpuid_get_raw_data(&raw) < 0) {
        printf("cpuid_get_raw_data failed\n");
        printf("Error: %s\n", cpuid_error());
        exit(EXIT_FAILURE);
    }
    if (cpu_identify(&raw, &data) < 0) {
        printf("cpu_identify failed\n");
        printf("Error: %s\n", cpuid_error());
        exit(EXIT_FAILURE);
    }
    nprocs = data.num_cores;
#else
    FILE *f;
    char line[128];

    if (!fopen("/proc/cpuinfo", "rb"))
        return nprocs;

    while (!feof(f)) {
        fgets(line, 128, f);
        if (strlen(line) == 1) continue;
        if (strncmp(line, "cpu cores", 8) == 0) {
            sscanf(line, "cpu cores\t: %d", &nprocs);
            fclose(f);
            return nprocs;
        }
    }
    fclose(f);
#endif
    return nprocs;
}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
	int parseres = parse_cmdline(argc, argv);
	int i, readres, writeres;
	int only_clock_queries;
	struct cpu_raw_data_t raw;
	struct cpu_id_t data;

	if (parseres != 1)
		return parseres;

	/* In quiet mode, disable libcpuid warning messages: */
	if (need_quiet)
		cpuid_set_warn_function(NULL);
	
	cpuid_set_verbosiness_level(verbose_level);

	/* Redirect output, if necessary: */
	if (strcmp(out_file, "") && strcmp(out_file, "-")) {
		fout = fopen(out_file, "wt");
		if (!fout) {
			if (!need_quiet)
				fprintf(stderr, "Cannot open `%s' for writing!\n", out_file);
			return -1;
		}
		atexit(close_out);
	} else {
		fout = stdout;
	}
	
	/* If requested, print library version: */
	if (need_version)
		fprintf(fout, "%s\n", cpuid_lib_version());
	
	if (need_input) {
		/* We have a request to input raw CPUID data from file: */
		if (!strcmp(raw_data_file, "-"))
			/* Input from stdin */
			readres = cpuid_deserialize_raw_data(&raw, "");
		else
			/* Input from file */
			readres = cpuid_deserialize_raw_data(&raw, raw_data_file);
		if (readres < 0) {
			if (!need_quiet) {
				fprintf(stderr, "Cannot deserialize raw data from ");
				if (!strcmp(raw_data_file, "-"))
					fprintf(stderr, "stdin\n");
				else
					fprintf(stderr, "file `%s'\n", raw_data_file);
				/* Print the error message */
				fprintf(stderr, "Error: %s\n", cpuid_error());
			}
			return -1;
		}
	} else {
		if (check_need_raw_data()) {
			/* Try to obtain raw CPUID data from the CPU: */
			readres = cpuid_get_raw_data(&raw);
			if (readres < 0) {
				if (!need_quiet) {
					fprintf(stderr, "Cannot obtain raw CPU data!\n");
					fprintf(stderr, "Error: %s\n", cpuid_error());
				}
				return -1;
			}
		}
	}
	
	/* Need to dump raw CPUID data to file: */
	if (need_output) {
		if (verbose_level >= 1)
			printf("Writing raw CPUID dump to `%s'\n", raw_data_file);
		if (!strcmp(raw_data_file, "-"))
			/* Serialize to stdout */
			writeres = cpuid_serialize_raw_data(&raw, "");
		else
			/* Serialize to file */
			writeres = cpuid_serialize_raw_data(&raw, raw_data_file);
		if (writeres < 0) {
			if (!need_quiet) {
				fprintf(stderr, "Cannot serialize raw data to ");
				if (!strcmp(raw_data_file, "-"))
					fprintf(stderr, "stdout\n");
				else
					fprintf(stderr, "file `%s'\n", raw_data_file);
				/* Print the error message */
				fprintf(stderr, "Error: %s\n", cpuid_error());
			}
			return -1;
		}
	}
	if (need_report) {
		if (verbose_level >= 1) {
			printf("Writing decoded CPU report to `%s'\n", out_file);
		}
		/* Write a thorough report of cpu_id_t structure to output (usually stdout) */
		fprintf(fout, "CPUID is present\n");
		/*
		 * Try CPU identification
		 * (this fill the `data' structure with decoded CPU features)
		 */
		if (cpu_identify(&raw, &data) < 0)
			fprintf(fout, "Error identifying the CPU: %s\n", cpuid_error());
		
		/* OK, now write what we have in `data'...: */
		fprintf(fout, "CPU Info:\n------------------\n");
		fprintf(fout, "  vendor_str : `%s'\n", data.vendor_str);
		fprintf(fout, "  vendor id  : %d\n", (int) data.vendor);
		fprintf(fout, "  brand_str  : `%s'\n", data.brand_str);
		fprintf(fout, "  family     : %d (%02Xh)\n", data.family, data.family);
		fprintf(fout, "  model      : %d (%02Xh)\n", data.model, data.model);
		fprintf(fout, "  stepping   : %d (%02Xh)\n", data.stepping, data.stepping);
		fprintf(fout, "  ext_family : %d (%02Xh)\n", data.ext_family, data.ext_family);
		fprintf(fout, "  ext_model  : %d (%02Xh)\n", data.ext_model, data.ext_model);
		fprintf(fout, "  num_cores  : %d\n", data.num_cores);
		fprintf(fout, "  num_logical: %d\n", data.num_logical_cpus);
		fprintf(fout, "  tot_logical: %d\n", data.total_logical_cpus);
		fprintf(fout, "  L1 D cache : %d KB\n", data.l1_data_cache);
		fprintf(fout, "  L1 I cache : %d KB\n", data.l1_instruction_cache);
		fprintf(fout, "  L2 cache   : %d KB\n", data.l2_cache);
		fprintf(fout, "  L3 cache   : %d KB\n", data.l3_cache);
		fprintf(fout, "  L4 cache   : %d KB\n", data.l4_cache);
		fprintf(fout, "  L1D assoc. : %d-way\n", data.l1_assoc);
		fprintf(fout, "  L2 assoc.  : %d-way\n", data.l2_assoc);
		fprintf(fout, "  L3 assoc.  : %d-way\n", data.l3_assoc);
		fprintf(fout, "  L4 assoc.  : %d-way\n", data.l4_assoc);
		fprintf(fout, "  L1D line sz: %d bytes\n", data.l1_cacheline);
		fprintf(fout, "  L2 line sz : %d bytes\n", data.l2_cacheline);
		fprintf(fout, "  L3 line sz : %d bytes\n", data.l3_cacheline);
		fprintf(fout, "  L4 line sz : %d bytes\n", data.l4_cacheline);
		fprintf(fout, "  SSE units  : %d bits (%s)\n", data.sse_size, data.detection_hints[CPU_HINT_SSE_SIZE_AUTH] ? "authoritative" : "non-authoritative");
		fprintf(fout, "  code name  : `%s'\n", data.cpu_codename);
		fprintf(fout, "  features   :");
		/*
		 * Here we enumerate all CPU feature bits, and when a feature
		 * is present output its name:
		 */
		for (i = 0; i < NUM_CPU_FEATURES; i++)
			if (data.flags[i])
				fprintf(fout, " %s", cpu_feature_str(i));
		fprintf(fout, "\n");
		
		/* Is CPU clock info requested? */
		if (need_clockreport) {
			if (need_timed_clockreport) {
				/* Here we use the RDTSC-based routine */
				fprintf(fout, "  cpu clock  : %d MHz\n",
				        cpu_clock_measure(400, 1));
			} else {
				/* Here we use the OS-provided info */
				fprintf(fout, "  cpu clock  : %d MHz\n",
				        cpu_clock());
			}
		}
	}
	/*
	 * Check if we have any queries to process.
	 * We have to handle the case when `--clock' or `--clock-rdtsc' options
	 * are present.
	 * If in report mode, this will generate spurious output after the
	 * report, if not handled explicitly.
	 */
	only_clock_queries = 1;
	for (i = 0; i < num_requests; i++)
		if (requests[i] != NEED_CLOCK && requests[i] != NEED_CLOCK_RDTSC) {
			only_clock_queries = 0;
			break;
		}
	/* OK, process all queries. */
	if ((!need_report || !only_clock_queries) && num_requests > 0) {
		/* Identify the CPU. Make it do cpuid_get_raw_data() itself */
		if (check_need_raw_data() && cpu_identify(&raw, &data) < 0) {
			if (!need_quiet)
				fprintf(stderr,
				        "Error identifying the CPU: %s\n",
				        cpuid_error());
			return -1;
		}
		for (i = 0; i < num_requests; i++)
			print_info(requests[i], &raw, &data);
	}
	if (need_cpulist) {
		print_cpulist();
	}
	
	return 0;
}
Exemplo n.º 3
0
static void print_info(output_data_switch query, struct cpu_raw_data_t* raw,
                       struct cpu_id_t* data)
{
	int i, value;
	struct msr_driver_t* handle;
	switch (query) {
		case NEED_CPUID_PRESENT:
			fprintf(fout, "%d\n", cpuid_present());
			break;
		case NEED_VENDOR_STR:
			fprintf(fout, "%s\n", data->vendor_str);
			break;
		case NEED_VENDOR_ID:
			fprintf(fout, "%d\n", data->vendor);
			break;
		case NEED_BRAND_STRING:
			fprintf(fout, "%s\n", data->brand_str);
			break;
		case NEED_FAMILY:
			fprintf(fout, "%d\n", data->family);
			break;
		case NEED_MODEL:
			fprintf(fout, "%d\n", data->model);
			break;
		case NEED_STEPPING:
			fprintf(fout, "%d\n", data->stepping);
			break;
		case NEED_EXT_FAMILY:
			fprintf(fout, "%d\n", data->ext_family);
			break;
		case NEED_EXT_MODEL:
			fprintf(fout, "%d\n", data->ext_model);
			break;
		case NEED_NUM_CORES:
			fprintf(fout, "%d\n", data->num_cores);
			break;
		case NEED_NUM_LOGICAL:
			fprintf(fout, "%d\n", data->num_logical_cpus);
			break;
		case NEED_TOTAL_CPUS:
			fprintf(fout, "%d\n", cpuid_get_total_cpus());
			break;
		case NEED_L1D_SIZE:
			fprintf(fout, "%d\n", data->l1_data_cache);
			break;
		case NEED_L1I_SIZE:
			fprintf(fout, "%d\n", data->l1_instruction_cache);
			break;
		case NEED_L2_SIZE:
			fprintf(fout, "%d\n", data->l2_cache);
			break;
		case NEED_L3_SIZE:
			fprintf(fout, "%d\n", data->l3_cache);
			break;
		case NEED_L4_SIZE:
			fprintf(fout, "%d\n", data->l4_cache);
			break;
		case NEED_L1D_ASSOC:
			fprintf(fout, "%d\n", data->l1_assoc);
			break;
		case NEED_L2_ASSOC:
			fprintf(fout, "%d\n", data->l2_assoc);
			break;
		case NEED_L3_ASSOC:
			fprintf(fout, "%d\n", data->l3_assoc);
			break;
		case NEED_L4_ASSOC:
			fprintf(fout, "%d\n", data->l4_assoc);
			break;
		case NEED_L1D_CACHELINE:
			fprintf(fout, "%d\n", data->l1_cacheline);
			break;
		case NEED_L2_CACHELINE:
			fprintf(fout, "%d\n", data->l2_cacheline);
			break;
		case NEED_L3_CACHELINE:
			fprintf(fout, "%d\n", data->l3_cacheline);
			break;
		case NEED_L4_CACHELINE:
			fprintf(fout, "%d\n", data->l4_cacheline);
			break;
		case NEED_CODENAME:
			fprintf(fout, "%s\n", data->cpu_codename);
			break;
		case NEED_FEATURES:
		{
			for (i = 0; i < NUM_CPU_FEATURES; i++)
				if (data->flags[i])
					fprintf(fout, " %s", cpu_feature_str(i));
			fprintf(fout, "\n");
			break;
		}
		case NEED_CLOCK:
			fprintf(fout, "%d\n", cpu_clock());
			break;
		case NEED_CLOCK_OS:
			fprintf(fout, "%d\n", cpu_clock_by_os());
			break;
		case NEED_CLOCK_RDTSC:
			fprintf(fout, "%d\n", cpu_clock_measure(400, 1));
			break;
		case NEED_CLOCK_IC:
			fprintf(fout, "%d\n", cpu_clock_by_ic(25, 16));
			break;
		case NEED_RDMSR:
		{
			if ((handle = cpu_msr_driver_open()) == NULL) {
				fprintf(fout, "Cannot open MSR driver: %s\n", cpuid_error());
			} else {
				if ((value = cpu_msrinfo(handle, INFO_MPERF)) != CPU_INVALID_VALUE)
					fprintf(fout, "  MSR.mperf  : %d MHz\n", value);
				if ((value = cpu_msrinfo(handle, INFO_APERF)) != CPU_INVALID_VALUE)
					fprintf(fout, "  MSR.aperf  : %d MHz\n", value);
				if ((value = cpu_msrinfo(handle, INFO_CUR_MULTIPLIER)) != CPU_INVALID_VALUE)
					fprintf(fout, "  cur. multi.: %d MHz\n", value);
				if ((value = cpu_msrinfo(handle, INFO_MAX_MULTIPLIER)) != CPU_INVALID_VALUE)
					fprintf(fout, "  max. multi.: %d MHz\n", value);
				if ((value = cpu_msrinfo(handle, INFO_TEMPERATURE)) != CPU_INVALID_VALUE)
					fprintf(fout, "  temperature: %d degrees Celsius\n", value);
				if ((value = cpu_msrinfo(handle, INFO_THROTTLING)) != CPU_INVALID_VALUE)
					fprintf(fout, "  throttling : %s\n", value ? "yes" : "no");
				if ((value = cpu_msrinfo(handle, INFO_VOLTAGE)) != CPU_INVALID_VALUE)
					fprintf(fout, "  core volt. : %.2lf Volts\n", value / 100.0);
				if ((value = cpu_msrinfo(handle, INFO_BCLK)) != CPU_INVALID_VALUE)
					fprintf(fout, "  base clock : %.2lf MHz\n", value / 100.0);
				cpu_msr_driver_close(handle);
			}
			break;
		}
		case NEED_SSE_UNIT_SIZE:
		{
			fprintf(fout, "%d (%s)\n", data->sse_size, 
				data->detection_hints[CPU_HINT_SSE_SIZE_AUTH] ? "authoritative" : "non-authoritative");
			break;
		}
		default:
			fprintf(fout, "How did you get here?!?\n");
			break;
	}
}