FreeBSD/usr.sbin/powerd/powerd.c
/*
 * This function returns summary load of all CPUs.  It was made so
 * intentionally to not reduce performance in scenarios when several
 * threads are processing requests as a pipeline -- running one at
 * a time on different CPUs and waiting for each other.
 */
This function returns summary load of all CPUs.  It was made so
intentionally to not reduce performance in scenarios when several
threads are processing requests as a pipeline -- running one at
a time on different CPUs and waiting for each other.
This function returns summary load of all CPUs . It was made so intentionally to not reduce performance in scenarios when several threads are processing requests as a pipeline -- running one at a time on different CPUs and waiting for each other .
static int
read_usage_times(int *load)
{
	static long *cp_times = NULL, *cp_times_old = NULL;
	static int ncpus = 0;
	size_t cp_times_len;
	int error, cpu, i, total;
	if (cp_times == NULL) {
		cp_times_len = 0;
		error = sysctl(cp_times_mib, 2, NULL, &cp_times_len, NULL, 0);
		if (error)
			return (error);
		if ((cp_times = malloc(cp_times_len)) == NULL)
			return (errno);
		if ((cp_times_old = malloc(cp_times_len)) == NULL) {
			free(cp_times);
			cp_times = NULL;
			return (errno);
		}
		ncpus = cp_times_len / (sizeof(long) * CPUSTATES);
	}
	cp_times_len = sizeof(long) * CPUSTATES * ncpus;
	error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
	if (error)
		return (error);
	if (load) {
		*load = 0;
		for (cpu = 0; cpu < ncpus; cpu++) {
			total = 0;
			for (i = 0; i < CPUSTATES; i++) {
			    total += cp_times[cpu * CPUSTATES + i] -
				cp_times_old[cpu * CPUSTATES + i];
			}
			if (total == 0)
				continue;
			*load += 100 - (cp_times[cpu * CPUSTATES + CP_IDLE] -
			    cp_times_old[cpu * CPUSTATES + CP_IDLE]) * 100 / total;
		}
	}
	memcpy(cp_times_old, cp_times, cp_times_len);
	return (0);
}
static int read_usage_times ( int * load ) { static long * cp_times = NULL , * cp_times_old = NULL ; static int ncpus = 0 ; size_t cp_times_len ; int error , cpu , i , total ; if ( cp_times == NULL ) { cp_times_len = 0 ; error = sysctl ( cp_times_mib , 2 , NULL , & cp_times_len , NULL , 0 ) ; if ( error ) return ( error ) ; if ( ( cp_times = malloc ( cp_times_len ) ) == NULL ) return ( errno ) ; if ( ( cp_times_old = malloc ( cp_times_len ) ) == NULL ) { free ( cp_times ) ; cp_times = NULL ; return ( errno ) ; } ncpus = cp_times_len / ( sizeof ( long ) * CPUSTATES ) ; } cp_times_len = sizeof ( long ) * CPUSTATES * ncpus ; error = sysctl ( cp_times_mib , 2 , cp_times , & cp_times_len , NULL , 0 ) ; if ( error ) return ( error ) ; if ( load ) { * load = 0 ; for ( cpu = 0 ; cpu < ncpus ; cpu ++ ) { total = 0 ; for ( i = 0 ; i < CPUSTATES ; i ++ ) { total += cp_times [ cpu * CPUSTATES + i ] - cp_times_old [ cpu * CPUSTATES + i ] ; } if ( total == 0 ) continue ; * load += 100 - ( cp_times [ cpu * CPUSTATES + CP_IDLE ] - cp_times_old [ cpu * CPUSTATES + CP_IDLE ] ) * 100 / total ; } } memcpy ( cp_times_old , cp_times , cp_times_len ) ; return ( 0 ) ; }
This function returns summary load of all CPUs.
This function returns summary load of all CPUs .