⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_divers.c

📁 一个用于智能手机的多媒体库适合S60 WinCE的跨平台开发库
💻 C
📖 第 1 页 / 共 3 页
字号:
	u32 nb_threads, entry_time;	HANDLE hSnapShot;	assert(sys_init);	if (!rti) return 0;	proc_idle_time = proc_k_u_time = process_k_u_time = 0;	nb_threads = 0;	entry_time = gf_sys_clock();	if (last_update_time && (entry_time - last_update_time < refresh_time_ms)) {		memcpy(rti, &the_rti, sizeof(GF_SystemRTInfo));		return 0;	}	if (flags & GF_RTI_SYSTEM_MEMORY_ONLY) {		memset(rti, 0, sizeof(GF_SystemRTInfo));		rti->sampling_instant = last_update_time;		GlobalMemoryStatus(&ms);		rti->physical_memory = ms.dwTotalPhys;		rti->physical_memory_avail = ms.dwAvailPhys;		rti->gpac_memory = (u64) gpac_allocated_memory;//		fprintf(stdout, "Total memory (nbblocs*4+memory) %d\n", 4*gpac_nb_alloc_blocs+gpac_allocated_memory);		return 1;	}#if defined (_WIN32_WCE)	total_cpu_time = process_cpu_time = 0;	/*get a snapshot of all running threads*/	orig_perm = GetCurrentPermissions();	SetProcPermissions(0xFFFFFFFF);	hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); 	if (!hSnapShot) return 0;	tentry.dwSize = sizeof(THREADENTRY32); 	the_rti.thread_count = 0;	/*note we always act as if GF_RTI_ALL_PROCESSES_TIMES flag is set, since there is no other way	to enumerate threads from a process, and GetProcessTimes doesn't exist on CE*/	if (Thread32First(hSnapShot, &tentry)) {		do {			/*get thread times*/			if (GetThreadTimes( (HANDLE) tentry.th32ThreadID, (FILETIME *) &creation, (FILETIME *) &exit, (FILETIME *) &kernel, (FILETIME *) &user)) {				total_cpu_time += user + kernel;				if (tentry.th32OwnerProcessID==the_rti.pid) {					process_cpu_time += user + kernel;					the_rti.thread_count ++;				}			}		} while (Thread32Next(hSnapShot, &tentry));	}	CloseToolhelp32Snapshot(hSnapShot); 	if (flags & GF_RTI_PROCESS_MEMORY) {		HEAPLIST32 hlentry;		HEAPENTRY32 hentry;		the_rti.process_memory = 0;		hlentry.dwSize = sizeof(HEAPLIST32); 		hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST, the_rti.pid); 		if (hSnapShot && Heap32ListFirst(hSnapShot, &hlentry)) {			do {				hentry.dwSize = sizeof(hentry);				if (Heap32First(hSnapShot, &hentry, hlentry.th32ProcessID, hlentry.th32HeapID)) {					do {						the_rti.process_memory += hentry.dwBlockSize;					} while (Heap32Next(hSnapShot, &hentry));				}			} while (Heap32ListNext(hSnapShot, &hlentry));		}		CloseToolhelp32Snapshot(hSnapShot); 	}	SetProcPermissions(orig_perm);	total_cpu_time /= 10;	process_cpu_time /= 10;#else	/*XP-SP1 and Win2003 servers only have GetSystemTimes support. This will give a better estimation	of CPU usage since we can take into account the idle time*/	if (MyGetSystemTimes) {		u64 u_time;		MyGetSystemTimes(&proc_idle_time, &proc_k_u_time, &u_time);		proc_k_u_time += u_time;		proc_idle_time /= 10;		proc_k_u_time /= 10;	}	/*same rq for NtQuerySystemInformation*/	else if (MyQuerySystemInfo) {		DWORD ret;		SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION info;		MyQuerySystemInfo(0x8 /*SystemProcessorPerformanceInformation*/, &info, sizeof(info), &ret); 		if (ret && (ret<=sizeof(info))) {			proc_idle_time = info.IdleTime.QuadPart / 10;			proc_k_u_time = (info.KernelTime.QuadPart + info.UserTime.QuadPart) / 10;		}	}	/*no special API available, ONLY FETCH TIMES if requested (may eat up some time)*/	else if (flags & GF_RTI_ALL_PROCESSES_TIMES) {	    PROCESSENTRY32 pentry; 		/*get a snapshot of all running threads*/		hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 		if (!hSnapShot) return 0;		pentry.dwSize = sizeof(PROCESSENTRY32); 		if (Process32First(hSnapShot, &pentry)) {			do {				HANDLE procH = NULL;				if (pentry.th32ProcessID) procH = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pentry.th32ProcessID);				if (procH && GetProcessTimes(procH, (FILETIME *) &creation, (FILETIME *) &exit, (FILETIME *) &kernel, (FILETIME *) &user) ) {					user += kernel;					proc_k_u_time += user;					if (pentry.th32ProcessID==the_rti.pid) {						process_k_u_time = user;						nb_threads = pentry.cntThreads;					}				}				if (procH) CloseHandle(procH);			} while (Process32Next(hSnapShot, &pentry));		}		CloseHandle(hSnapShot); 		proc_k_u_time /= 10;	} 	if (!process_k_u_time) {		HANDLE procH = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, the_rti.pid);		if (procH && GetProcessTimes(procH, (FILETIME *) &creation, (FILETIME *) &exit, (FILETIME *) &kernel, (FILETIME *) &user) ) {			process_k_u_time = user + kernel;		}		if (procH) CloseHandle(procH);		if (!process_k_u_time) return 0;	}	process_k_u_time /= 10;	/*this won't cost a lot*/	if (MyGetProcessMemoryInfo) {		PROCESS_MEMORY_COUNTERS pmc;		HANDLE procH = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, the_rti.pid);		MyGetProcessMemoryInfo(procH, &pmc, sizeof (pmc));		the_rti.process_memory = pmc.WorkingSetSize;		if (procH) CloseHandle(procH);	}	/*THIS IS VERY HEAVY (eats up mem and time) - only perform if requested*/	else if (flags & GF_RTI_PROCESS_MEMORY) {		HEAPLIST32 hlentry;		HEAPENTRY32 hentry;		the_rti.process_memory = 0;		hlentry.dwSize = sizeof(HEAPLIST32); 		hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST, the_rti.pid); 		if (hSnapShot && Heap32ListFirst(hSnapShot, &hlentry)) {			do {				hentry.dwSize = sizeof(hentry);				if (Heap32First(&hentry, hlentry.th32ProcessID, hlentry.th32HeapID)) {					do {						the_rti.process_memory += hentry.dwBlockSize;					} while (Heap32Next(&hentry));				}			} while (Heap32ListNext(hSnapShot, &hlentry));		}		CloseHandle(hSnapShot); 	}#endif	the_rti.sampling_instant = last_update_time;	if (last_update_time) {		the_rti.sampling_period_duration = entry_time - last_update_time;		the_rti.process_cpu_time_diff = (u32) ((process_k_u_time - last_process_k_u_time)/1000);#if defined(_WIN32_WCE)		the_rti.total_cpu_time_diff = (u32) ((total_cpu_time - last_total_k_u_time)/1000);		/*we're not that accurate....*/		if (the_rti.total_cpu_time_diff > the_rti.sampling_period_duration) 			the_rti.sampling_period_duration = the_rti.total_cpu_time_diff;			/*rough values*/		the_rti.cpu_idle_time = the_rti.sampling_period_duration - the_rti.total_cpu_time_diff;		the_rti.total_cpu_usage = (u32) (100 * the_rti.total_cpu_time_diff / the_rti.sampling_period_duration);		the_rti.process_cpu_usage = (u32) (100*the_rti.process_cpu_time_diff / (the_rti.total_cpu_time_diff + the_rti.cpu_idle_time) );#else		/*oops, we have no choice but to assume 100% cpu usage during this period*/		if (!proc_k_u_time) {			the_rti.total_cpu_time_diff = the_rti.sampling_period_duration;			proc_k_u_time = last_proc_k_u_time + the_rti.sampling_period_duration;			the_rti.cpu_idle_time = 0;			the_rti.total_cpu_usage = 100;			if (the_rti.sampling_period_duration)				the_rti.process_cpu_usage = (u32) (100*the_rti.process_cpu_time_diff / the_rti.sampling_period_duration);		} else {			u64 samp_sys_time, idle;			the_rti.total_cpu_time_diff = (u32) ((proc_k_u_time - last_proc_k_u_time)/1000);			/*we're not that accurate....*/			if (the_rti.total_cpu_time_diff > the_rti.sampling_period_duration) {				the_rti.sampling_period_duration = the_rti.total_cpu_time_diff;			}						if (!proc_idle_time) 				proc_idle_time = last_proc_idle_time + (the_rti.sampling_period_duration - the_rti.total_cpu_time_diff); 			samp_sys_time = proc_k_u_time - last_proc_k_u_time;			idle = proc_idle_time - last_proc_idle_time;			the_rti.cpu_idle_time = (u32) (idle/1000);			if (samp_sys_time) {				the_rti.total_cpu_usage = (u32) ( (samp_sys_time - idle) / (samp_sys_time / 100) );				the_rti.process_cpu_usage = (u32) (100*the_rti.process_cpu_time_diff / (samp_sys_time/1000));			}		}#endif	}	last_update_time = entry_time;	last_process_k_u_time = process_k_u_time;	GlobalMemoryStatus(&ms);	the_rti.physical_memory = ms.dwTotalPhys;	the_rti.gpac_memory = (u64) gpac_allocated_memory;	the_rti.physical_memory_avail = ms.dwAvailPhys;#if defined(_WIN32_WCE)		last_total_k_u_time = total_cpu_time;	if (!the_rti.process_memory) the_rti.process_memory = mem_usage_at_startup - ms.dwAvailPhys;#else	last_proc_idle_time = proc_idle_time;	last_proc_k_u_time = proc_k_u_time;#endif	memcpy(rti, &the_rti, sizeof(GF_SystemRTInfo));	return 1;}#elseBool gf_sys_get_rti(u32 refresh_time_ms, GF_SystemRTInfo *rti, u32 flags){  u32 entry_time;  u64 process_u_k_time;  u32 u_k_time, idle_time;#if 0  char szProc[100];#endif  char line[2048];  FILE *f;  assert(sys_init);  entry_time = gf_sys_clock();  if (last_update_time && (entry_time - last_update_time < refresh_time_ms)) {    memcpy(rti, &the_rti, sizeof(GF_SystemRTInfo));    return 0;  }  u_k_time = idle_time = 0;  f = fopen("/proc/stat", "r");  if (f) {    u32 k_time, nice_time, u_time;    if (fgets(line, 128, f) != NULL) {      if (sscanf(line, "cpu  %u %u %u %u\n", &u_time, &k_time, &nice_time, &idle_time) == 4) {	u_k_time = u_time + k_time + nice_time;      }    }    fclose(f);  }  process_u_k_time = 0;  the_rti.process_memory = 0;  /*FIXME? under LinuxThreads this will only fetch stats for the calling thread, we would have to enumerate /proc to get   the complete CPU usage of all therads of the process...*/#if 0  sprintf(szProc, "/proc/%d/stat", the_rti.pid);  f = fopen(szProc, "r");  if (f) {    fflush(f);    if (fgets(line, 2048, f) != NULL) {      char state;      char *start;      long cutime, cstime, priority, nice, itrealvalue, rss;      int exit_signal, processor;      unsigned long flags, minflt, cminflt, majflt, cmajflt, utime, stime,starttime, vsize, rlim, startcode, endcode, startstack, kstkesp, kstkeip, signal, blocked, sigignore, sigcatch, wchan, nswap, cnswap, rem;      int ppid, pgrp ,session, tty_nr, tty_pgrp, res;      start = strchr(line, ')');      if (start) start += 2;      else {	start = strchr(line, ' ');	start++;      }      res = sscanf(start,"%c %d %d %d %d %d %lu %lu %lu %lu \%lu %lu %lu %ld %ld %ld %ld %ld %ld %lu \%lu %ld %lu %lu %lu %lu %lu %lu %lu %lu \%lu %lu %lu %lu %lu %d %d",		   &state, &ppid, &pgrp, &session, &tty_nr, &tty_pgrp, &flags, &minflt, &cminflt, &majflt,		   &cmajflt, &utime, &stime, &cutime, &cstime, &priority, &nice, &itrealvalue, &rem, &starttime,		   &vsize, &rss, &rlim, &startcode, &endcode, &startstack, &kstkesp, &kstkeip, &signal, &blocked,		   &sigignore, &sigcatch, &wchan, &nswap, &cnswap, &exit_signal, &processor);       if (res) process_u_k_time = (u64) (cutime + cstime);      else {		GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[RTI] PROC %s parse error\n", szProc));	  }    } else {		GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[RTI] error reading pid/stat\n\n", szProc));    }    fclose(f);  } else {	GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[RTI] cannot open %s\n", szProc));  }  sprintf(szProc, "/proc/%d/status", the_rti.pid);  f = fopen(szProc, "r");  if (f) {    while (fgets(line, 1024, f) != NULL) {      if (!strnicmp(line, "VmSize:", 7)) {	sscanf(line, "VmSize: %lld kB",  &the_rti.process_memory);	the_rti.process_memory *= 1024;      }    }    fclose(f);  } else {	GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[RTI] cannot open %s\n", szProc));  }#endif  the_rti.physical_memory = the_rti.physical_memory_avail = 0;  f = fopen("/proc/meminfo", "r");  if (f) {    while (fgets(line, 1024, f) != NULL) {      if (!strnicmp(line, "MemTotal:", 9)) {	sscanf(line, "MemTotal: %lld kB",  &the_rti.physical_memory);	the_rti.physical_memory *= 1024;      }else if (!strnicmp(line, "MemFree:", 8)) {	sscanf(line, "MemFree: %lld kB",  &the_rti.physical_memory_avail);	the_rti.physical_memory_avail *= 1024;	break;      }    }    fclose(f);  } else {	GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[RTI] cannot open /proc/meminfo\n"));  }  the_rti.sampling_instant = last_update_time;    if (last_update_time) {    the_rti.sampling_period_duration = (entry_time - last_update_time);    the_rti.process_cpu_time_diff = (process_u_k_time - last_process_k_u_time) * 10;    /*oops, we have no choice but to assume 100% cpu usage during this period*/    if (!u_k_time) {      the_rti.total_cpu_time_diff = the_rti.sampling_period_duration;      u_k_time = last_cpu_u_k_time + the_rti.sampling_period_duration;      the_rti.cpu_idle_time = 0;      the_rti.total_cpu_usage = 100;      if (!the_rti.process_cpu_time_diff) the_rti.process_cpu_time_diff = the_rti.total_cpu_time_diff;      the_rti.process_cpu_usage = (u32) ( 100 *  the_rti.process_cpu_time_diff / the_rti.sampling_period_duration);    } else {      u64 samp_sys_time;      /*move to ms (/proc/stat gives times in 100 ms unit*/      the_rti.total_cpu_time_diff = (u_k_time - last_cpu_u_k_time)*10;      /*we're not that accurate....*/      if (the_rti.total_cpu_time_diff > the_rti.sampling_period_duration)      	the_rti.sampling_period_duration = the_rti.total_cpu_time_diff;         if (!idle_time) idle_time = (the_rti.sampling_period_duration - the_rti.total_cpu_time_diff)/10;      samp_sys_time = u_k_time - last_cpu_u_k_time;      the_rti.cpu_idle_time = idle_time - last_cpu_idle_time;      the_rti.total_cpu_usage = (u32) ( 100 * samp_sys_time / (the_rti.cpu_idle_time + samp_sys_time ) );      /*move to ms (/proc/stat gives times in 100 ms unit*/      the_rti.cpu_idle_time *= 10;      if (!the_rti.process_cpu_time_diff) the_rti.process_cpu_time_diff = the_rti.total_cpu_time_diff;      the_rti.process_cpu_usage = (u32) ( 100 *  the_rti.process_cpu_time_diff / (the_rti.cpu_idle_time + 10*samp_sys_time ) );    }  } else {    mem_at_startup = the_rti.physical_memory_avail;  }  the_rti.process_memory = mem_at_startup - the_rti.physical_memory_avail;  the_rti.gpac_memory = gpac_allocated_memory;  last_process_k_u_time = process_u_k_time;  last_cpu_idle_time = idle_time;  last_cpu_u_k_time = u_k_time;  last_update_time = entry_time;  memcpy(rti, &the_rti, sizeof(GF_SystemRTInfo));  return 1;}#endifBool gf_sys_get_battery_state(Bool *onBattery, u32 *state, u32*level) {#if defined(WIN32) && !defined(_WIN32_WCE)	SYSTEM_POWER_STATUS sps;	if (!onBattery || !state || !level) return 0;	GetSystemPowerStatus(&sps);	*onBattery = sps.ACLineStatus;	*state = sps.BatteryFlag;	*level = sps.BatteryLifePercent;#endif	return 1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -