用户工具

站点工具


bit010:2_task:4_多线程内获取线程时间
inline float get_sum_exec_runtime(int tid) {
  usleep(0); // flush kernal::se.sum_exec_runtime

  char filename[1024];
  sprintf(filename, "/proc/%d/sched", tid);
  FILE *pFile=fopen(filename, "r");
  if(pFile == NULL) return -1.0f;
  char info[512] = {0,};
  int rtn0 = fseek(pFile, 0, SEEK_SET);
  int rtn1 = fread(info, 1, sizeof(info), pFile);
  fclose(pFile);

  char keyword[]="se.sum_exec_runtime";
	
  float time_try = 0.0f;

  if (1) {
    char *info_pt = info;
    char *info_end_pt = &info[sizeof(info) - 1 - sizeof(keyword)];

	int isFind = 0;
    while(isFind == 0) {		
      info_pt++;
	  if(info_pt == info_end_pt) break;
	  
      if(*(int*)&info_pt[0] != *(int*)&keyword[0]) continue;
      if(memcmp(info_pt, &keyword[0], sizeof(keyword)-1)) continue;
      while(1) {
	  	if(info_pt == info_end_pt) break;
		if((info_pt[0]>='0') && (info_pt[0]<='9')) {
		  isFind = 1;
		  break;
		}
	  	info_pt++;
      }
    }

	if(isFind == 1) {
      int rtn2 = sscanf(info_pt, "%f", &time_try);
      if(rtn2 == 1) {
        return time_try; 
      } else {
        return -2.0f;
      }
    }
  } 
  return -5.0f;
}
bit010/2_task/4_多线程内获取线程时间.txt · 最后更改: 2025/05/29 15:28 由 127.0.0.1