In free BSD in C, how does a CPU use use?
I am writing some code to handle HTTP redirection if CPU load goes beyond the limit on a FReeBSD system, then I want to redirect client requests. Searching on the man page, kvm_getpcpu () seems correct, but do not document man page (which I have read).
Any suggestions or hints have been given - Thanks!
After reading the answer here, I was able to come down with due to poor documentation, I agree 100% not sure that this is correct, but the top agree. Thanks for all the people.
#include & lt; Stdio.h & gt; #include & lt; String.h & gt; # Include & lt; Sys / types.h & gt; # Include & lt; Sys / sysctl.h & gt; # Include & lt; Unistd.h & gt; #define CP_USER 0 #define CP_NICE 1 #define CP_SYS 2 #define CP_INTR 3 #define CP_IDLE Define 4 # 5 Int main () {long [Kmbuddhi] last [Seepitiattia]; Size_t cur_sz = Size size; Int State, I; Long amount; Double use; Memeset (last, 0, last of size); For (i = 0; i & lt; 6; i ++) {if (sysctlbyname ( "kern.cp_time", & amp; cur, & amp; cur_sz, NULL, 0) & lt; 0) {printf ( "kern.cp_times reading sysctl error" n "); return -1;} Sum = 0; (state = 0; state & lt; Seepitiattis; state ++) {long TMP = CR [state]; Q [ State] - last [state]; last [state] = tmp; sum + = cur [state];} Util = 100.0L - (100.0L * cur [CP_IDLE] / (yoga (double) sum: 1.0 L)) ; Printf ("CPU usage:% 7.3f \ n", use); Sleep (1);} Return 0;} << / P> NAME
/ Lt>
Lkvm)
< Strong> SYNOPSIS
#include & Lt; sys / param.h & gt; # Include & lt; sys / pcpu.h & gt; # Include & lt; sys / sysctl.h & gt; # Include & lt; kvm.h & gt; Int kvm_getmaxcpu (kvm_t * kd); void * kvm_getpcpu (kvm_t * kd, int cpu); description kvm_getmaxcpu () and kvm_getpcpu () function to per-CPU data reported by used to reach the per-CPU data of active processor kernel supported by Kvm_getmaxcpu () function in the kernel gives the maximum number of CPU to a CPU Kvm_getpcpu () function Give buffer Is obtained. This buffer is described by the protected pcpu type. Caller is responsible for releasing buffer through a call for free (3) when it is not needed if the CPU is not active, then instead of returning to the tap.
Caching These functions cache the nlist values for different kernel variables that are reuse in consecutive calls.
returns Valian On success, kvm_getmaxcpu () function returns the maximum number an error occurs if supported by the kernel CPU, so instead of 1 Returns.
On success, the kvm_getpcpu () function returns a pointer to the allocated buffer or tap. If an error occurs, then it returns in return for 1.
If a function encounters an error, an error message can be obtained by kvm_geterr (3.)
Edit Here is the kvm_t struct:
struct __kvm {/ * * The string to be added in the error message * provided for compatibility with the interface of the sun * If this value is empty, then error is saved in the errbuf [] * / const char * program; Four * errp; / * XXX This might go away * / Four fault [_POSIX2_LINE_MAX]; # Defined isylive (kd) (kd) -> gt; vmfd = 0) int pmfd; / * Physical memory file (or crashdump) * / int vmfd; / * Virtual Memory File (-1 if crashdump) * / int unused; / * Was: swap file (e.g., / dev / drum) * / int nlfd; / * Name file (e.g., / kernel) * / struct kinfo_proc * procbase; Char * argspc; / * (Dynamic) storage for argv string * / int arglen; / * Above * / four ** argv; Length of; / * (Dynamic) storage for argent pointers * / int argc; / * Above length (actual # is not present) * / Four * Arguf; / * (Dynamic) temporary storage * / / * * kernel virtual address translation status. It only fills up for the dying kernel; Otherwise, the running kernel (i.e. kimam) * will translate for us. This can be big, so we * allocate it if necessary. * / Struct vmstate * vmst; };
Comments
Post a Comment