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

📄 myxm.c

📁 可以在安装xgen虚拟机的条件下
💻 C
字号:
/**
  * Project: xgen
  * Version: 1.0
  * Author: Dong Zhangye
  * Date: 2008-05-20
  */
 #include <stdlib.h>
 #include <stdio.h>
 #include <libvirt/libvirt.h>
 
 #define MAXID 50
 
  /* the hypervisor connection */
 static virConnectPtr conn = NULL;
 
 void closeConn()
  {
     if (conn != NULL)
         virConnectClose(conn);
 }
 
 void freeDom(virDomainPtr dom)
  {
     if (dom != NULL)
         virDomainFree(dom);
 }
 
 void getDomainInfo(int id)
  {
     virDomainPtr dom = NULL; 
     virDomainInfo info; 
     int ret;
     int i, j;
     long long cpu_start, cpu_end;
     int cpu_diff;
     float usage;
 
      /* Find the domain of the given id */
     dom = virDomainLookupByID(conn, id);
     if (dom == NULL)
      {
     fprintf(stderr, "Failed to find Domain %d ", id);
         freeDom(dom);
         closeConn();
     }
 
      /* Get the information of the domain */
     ret = virDomainGetInfo(dom, &info);
     if (ret < 0)
      {
     fprintf(stderr, "Failed to get information for Domain %d ", id);
     freeDom(dom);
     closeConn();
     }
 
      /* calculate the usage of CPU */
      /* get the start of CPUTime */
     ret = virDomainGetInfo(dom, &info);
      if (ret < 0) {
         fprintf(stderr, "Failed to get information for Domain %d ", id);
         freeDom(dom);
         closeConn();
     }
     cpu_start = info.cpuTime;
 
      /* wait 1 second */
     sleep(1);
 
      /* get the end of CPUTime */
     ret = virDomainGetInfo(dom, &info);
      if (ret < 0) {
         fprintf(stderr, "Failed to get information for Domain %d ", id);
         freeDom(dom);
         closeConn();
     }
     cpu_end = info.cpuTime;
 
      /* let cpu_diff be the change in cpuTime over 1 second */
      /* that means the real time_diff is 1 second */
      /* then %CPU used by the domain is cpu_diff/real_diff */
     cpu_diff = (cpu_end - cpu_start)/10000;
     usage = cpu_diff / (float)1000;
 
     printf("%d %.3f% %lu %s ", id, usage, info.memory/1024, virDomainGetName(dom));
     freeDom(dom);
 }
 
 int main()
  {
     int idcount;
     int i;
     int id;
     int ids[MAXID];
 
      /* NULL means connect to local Xen hypervisor */
     conn = virConnectOpenReadOnly(NULL);
     if (conn == NULL)
      {
         fprintf(stderr, "Failed to connect to hypervisor ");
         closeConn(); 
         return 0;
     }
 
      /* get the count of IDs and save these ID into ids[] */
     idcount = virConnectListDomains(conn, &ids[0], MAXID);
     if (idcount < 0)
      {
         fprintf(stderr, "Failed to list the domains ");
         closeConn();
         return 0;
     }
     printf("Totals: %d ", idcount);
 
     printf("ID CPU MEM NAME ");
 
      /* loop print the domain info by IDs */
     for (i = 0; i < idcount; i++)
      {
         id = ids[i];
         getDomainInfo(id);
     }
 
     return 0;
 }

⌨️ 快捷键说明

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