/****************************************************************
*filename:myvm.c
*author:hemmingway
time:2012年05月25日 星期五 17时46分39秒
*compile with:gcc -g -Wall myvm.c -o myvm -lvirt
****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>

#define MAXID 50

/*the data strucure of time*/
typedef struct _timeInfo
{
 long long cpu_time;
 struct timeval real_time;
}TIMEINFONODE;

/*the hypervisor connection*/
static virConnectPtr conn = NULL;

/*release the connecton of hypervisor*/
void closeConn()
{
 if(conn != NULL)
  virConnectClose(conn);
}

/*release the domain pointer*/
void freeDom(virDomainPtr dom)
{
 if(dom != NULL)
  virDomainFree(dom);
}

/*get start time of each domain*/
void getTimeInfo(int id,TIMEINFONODE *infos)
{
 virDomainPtr dom = NULL;
 virDomainInfo info;
 int ret;
 
 /*find the domain of the given id*/
 dom = virDomainLookupByID(conn,id);
 if(NULL == dom)
  {
   fprintf(stderr,"failed to find domain by id \'%d\'.\n",id);
   freeDom(dom);
   closeConn();
  }
  
 /*get the information of the domain*/
 ret = virDomainGetInfo(dom,&info);
 if(ret < 0)  /*-1 ?*/
  {
   fprintf(stderr,"failed to get informaton for domain \'%d\'.\n",id);
   freeDom(dom);
   closeConn();
  }
  
 /*retrive the start time*/
 if(gettimeofday(&(infos->real_time),NULL) == -1)
  {
   fprintf(stderr,"oh my god!failed to get start time.\n");
   return;
  }
 
 /**/
 infos->cpu_time = info.cpuTime;
 
 freeDom(dom);
}

void getDomainInfo(int id,TIMEINFONODE infos)
{
 virDomainPtr dom = NULL;
 virDomainInfo info;
 int ret;
 
 struct timeval realTime;
 int cpu_diff,real_diff;
 float usage;
 
  /*find the domain of the given id*/
 dom = virDomainLookupByID(conn,id);
 if(NULL == dom)
  {
   fprintf(stderr,"failed to find domain by id \'%d\'.\n",id);
   freeDom(dom);
   closeConn();
  }
  
 /*get the information of the domain*/
 ret = virDomainGetInfo(dom,&info);
 if(ret < 0)  /*-1 ?*/
  {
   fprintf(stderr,"failed to get informaton for domain \'%d\'.\n",id);
   freeDom(dom);
   closeConn();
  }
  
 /*retrive the start time*/
 if(gettimeofday(&(infos.real_time),NULL) == -1)
  {
   fprintf(stderr,"oh my god!failed to get start time.\n");
   return;
  }
  
  cpu_diff = (info.cpuTime - infos.cpu_time) / 10000;
  real_diff = 1000 * (realTime.tv_sec - infos.real_time.tv_sec) +
            (realTime.tv_usec - infos.real_time.tv_usec);
  usage = cpu_diff / (float)(real_diff);
  
  printf("%d\t%.3f\t%lu\t%lu\t%d\t%0x\t%s\n",
     id,usage,info.memory/1024,info.maxMem/1024,
    info.nrVirtCpu,info.state,virDomainGetName(dom));

freeDom(dom);
}

int main()
{
 int idCount;
 int i;
 int id;
 int ids[MAXID];
 char *host;
 unsigned long ver;
 
 virNodeInfo nodeinfo;
 
 TIMEINFONODE timeInfos[MAXID];
 
 printf("----------------------------------------\n");
 printf("      xen domain monitor vrsion 0.1     \n");
 printf("      build by somebody.                \n");
 printf("----------------------------------------\n");
 
 /*connect to local xen hypervisor*/
 conn = virConnectOpenReadOnly(NULL);
 if(conn == NULL)
  {
   fprintf(stderr,"failed to connect to xen hypervisor\n");
   closeConn();
   return 0;
  }

host = virConnectGetHostname(conn);
 printf("Hostname:%s\n",host);

printf("Virtualization type:%s\n",virConnectGetType(conn));
 virConnectGetVersion(conn, &ver);
 printf("Version:%lu\n",ver);

/*
 virConnectGetLibVersion(conn, &ver);
 printf("Libvirt Version:%lu\n",ver);
*/

virNodeGetInfo(conn, &nodeinfo);
 printf("==========node info===============\n");
 printf("Model:%s\n",nodeinfo.model);
 printf("Memory:%lukb\n",nodeinfo.memory); 
 printf("Number of CPUs:%u\n",nodeinfo.cpus);
 printf("MHz:%u\n",nodeinfo.mhz);
 printf("Number of NUMA nodes:%u\n",nodeinfo.nodes); 
 printf("===============================\n");

idCount = virConnectListDomains(conn,&ids[0],MAXID);
   if(idCount < 0)
    {
   fprintf(stderr,"failed to list domains\n");
   closeConn();
   return 0;
  }

printf("Domain Totals: %d\n", idCount);
  printf("ID\tCPU\tMEM\tMaxMEM\tVCPUs\tState\tNAME\n");  
  
     /* loop get the CPUtime info by IDs */ 
  for (i = 0; i < idCount; i++)
  {
   id = ids[i];
   getTimeInfo(id, &(timeInfos[i]));
  }
  
  sleep(1); //1 sec
  
  /* loop print the domain info and calculate the usage of cpus*/
  for (i = 0; i < idCount; i++)
  {
   id = ids[i];
   getDomainInfo(id, timeInfos[i]);
                     
  }
  
  printf("--------------------------------------------------------\n");
  closeConn();
  
  return 0;
}

test libvirt相关推荐

  1. KVM libvirt 虚拟机快照技术

    kvm+libvirt虚拟机快照浅析 浅析snapshots, blockcommit,blockpull 作者:Kashyap Chamarthy <kchamart#redhat.com&g ...

  2. libvirt(virt-install命令介绍)

    为什么80%的码农都做不了架构师?>>>    在使用kvm命令建立虚拟机时每次都要输入很长的命令,容易出现输入错误,可以使用libvirt管理虚拟机,libvirt支持kvm,xe ...

  3. Windows下Libvirt Java API使用教程(二)- 接口使用说明

    介绍完libvirt Java API的部署工作: <Windows下Libvirt Java API使用教程(一)- 开发环境部署> 接下来我们就介绍一下接口的使用和代码样例. libv ...

  4. [To be translated] Nova:libvirt image 的生命周期

    翻译自:http://www.pixelbeat.org/docs/openstack_libvirt_images/ The main stages of a Virtual Machine dis ...

  5. Windows下Libvirt Java API使用教程(三)- TLS认证访问和动态链接文件依赖

    之前已经介绍过了libvirt api的上手使用方式: <Windows下Libvirt Java API使用教程(二)- 接口使用说明> <Windows下Libvirt Java ...

  6. Libvirt — 4 种主机虚拟化网络模式

    目录 文章目录 目录 Libvirt 的网络模式 1.vSwitch 使用 Bridge 模式 2.vSwitch 使用 NAT 模式 3.vSwitch 使用 Routed 模式 4.vSwitch ...

  7. Libvirt — Live Migration 的实现原理

    目录 文章目录 目录 Libvirt 的 Live Migration 网络数据传输层 控制层 通过 libvirt 库实现虚拟机迁移的示例 KVM 的预拷贝(Pre-Copy)Live Migrat ...

  8. Libvirt 版本降级过程记录 4.5.0 to 3.9.0

    前言 搞 OpenStack 开发 Libvirt 版本会随着 OpenStack 版本切来切去的,记录一下 Libvirt 从 4.5 降级到 3.9.0 的过程. 步骤 直接 downgrade ...

  9. KVM — 与 QEMU 和 Libvirt 的关系

    目录 文章目录 目录 前言 KVM QEMU KVM 与 QEMU qemu-kvm Libvirt Libvirt 在 OpenStack 中的应用 前言 如果是刚开始接触虚拟机技术的话, 对上述的 ...

  10. libvirt 启动 qemu 的过程

    1 背景介绍 gdb 调试多任务程序时会有些麻烦: fork 之后没法同时跟踪父进程和子进程,如果在子进程里设置了一个 breakpoint,那么子进程将会收到一个 SIGTRAP 信号并退出.gdb ...

最新文章

  1. 一起走进计算机视觉的世界
  2. PHP框架性能不权威对比
  3. 互联网协议 — TCP — 拥塞控制(网络质量保障)
  4. MyAdapter Andriod
  5. 「名词」数据管理、数据治理、数据中心、数据中台、数据湖、数据资产等
  6. php 非阻塞post请求,PHP实现的CURL非阻塞调用类
  7. .Net Cancellable Task - APM异步超时机制扩展
  8. Kubernetes入门——深入浅出讲Docker
  9. 用ionic快速开发hybird App(已附源码,在下面+总结见解)
  10. vs里根据json快速创建对应类的方法
  11. 「LibreOJ NOI Round #2」不等关系 (dp+NTT分治)
  12. 用matlab拟合多元函,MatLab中多元拟合的函数是什么?
  13. Linux之ssh-agent命令
  14. java aes加密_springboot.X手册:防抓包?快速实现API接口数据加密
  15. javascript---不可靠的“undefined”
  16. 20135226黄坤信息安全系统设计基础期末总结
  17. 如何便捷的获取AD(Altium Designer)封装、以及如何从PCB工程中导出封装库、封装库安装
  18. latex导数_使用LaTeX语法编写数学公式(持续更新)
  19. matlab中函数imhist的用法
  20. Android ScrollView 长截屏

热门文章

  1. 多對多、單向一對多做ORM時最好用基于连接表的方式
  2. 基于RBAC的权限设计模型
  3. php菲波那切数列,php如何实现菲波那切数列
  4. Destroying Array(并查集)
  5. python统计分析--3.线性回归四种算法
  6. grouplens上的movielens数据集_ItemCF算法的Python实现,数据基于MovieLens
  7. android 百度移动搜索 url 参数,百度刷站内快排算法参数-百度搜索URL参数比较详解...
  8. latex绘图笔记(一)
  9. 深度学习(16)TensorFlow高阶操作五: 张量限幅
  10. php apache 配置文件,Linux下apache支持PHP配置文件的方法