test libvirt

/****************************************************************
*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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/243734.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

身体对腐朽灵魂的一次震撼:向京的雕塑与观众的行为

在上文所介绍的“个案——艺术批评中的艺术家”展览中&#xff0c;在一个小房间似的展厅里展出了女雕塑家向京的作品《你的身体》。一个女孩随意地仰躺在白色的床上&#xff0c;两腿叉开&#xff0c;双肢上扬&#xff0c;手里拿着一只没来及关闭的手机&#xff0c;像是刚刚挂了…

双口RAM

双口RAM IP配置 激励文件 `timescale 1ns / 1ps `define clk_period 20 module dpram_tb( );reg clock;reg [7:0]data;reg [7:0]rdaddress;reg [7:0]wraddress;reg wren;wire [7:0]q;integer i;dist_mem_gen_0 dist_mem_gen_0 (.a(data), // input wire [7 : 0] a.d…

64位Ubuntu kylin 16.04搭建tftp服务器

http://www.linuxidc.com/Linux/2016-07/133255.htm1. 安装$ apt install tftp-hpa tftpd-hpa2. 建立目录$ mkdir /tftpboot # 这是建立tftp传输目录。$ sudo chmod 0777 /tftpboot$ sudo touch test.txt # test.txt文件最好输入内容以便区分3. 配置# vi /etc/default/tftpd-hp…

游牧大地的诗意:看龙力游的草原油画

暮咏蒙古秘史 家乡的亲戚来电话说&#xff0c;今年呼伦贝尔的草特别的绿&#xff0c;羊肉也特别地鲜美。8月1日还是鄂温克自治旗成立50年大庆。可惜我没有时间回去&#xff0c;郁闷之余&#xff0c;贴几张我喜欢的画家龙力游的油画&#xff0c;略表思乡之情。 龙力游是湖南人&a…

看奥运之一:实力是最重要的

8月9日&#xff0c;陈燮霞在北京奥运会举重48公斤级决赛中&#xff0c;夺得金牌&#xff0c;这是中国代表团在本届奥运会上获得的首枚金牌。 记者&#xff1a;陈燮霞夺得中国首金&#xff0c;她终于不负众望。 兴安&#xff1a;对&#xff0c;其实在比赛之前&#xff0c;这枚金…

状态机实现按键消抖

状态机实现按键消抖 module key_filter(Clk, //50M时钟输入Rst_n, //模块复位key_in, //按键输入key_flag, //按键标志信号key_state //按键状态信号);input Clk;input Rst_n;input key_in;output reg key_flag;output reg key_state;localparamIDEL = 4b0001,FILTE…

看奥运之二:现场看男子体操团体决赛

昨天和女儿去国家体育馆看了北京奥运会男子体操团体决赛。第一次现场看体操比赛&#xff0c;第一次现场看世界大赛&#xff0c;场面可想而知&#xff0c;非常火暴。这是中国男子体操队员获得团体比赛冠军的一刹那。队员都非常激动&#xff0c;有人甚至喜极而泣。这也是2004年雅…

64位Ubuntu kylin 16.04搭建nfs网络文件系统

一 安装配置nfs服务 1.新建目录 mkdir /home/ning/rootfs 2.安装nfs服务 ningENVY:/$ sudo apt install nfs-kernel-server 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 将会同时安装下列软件&#xff1a; keyutil…

看奥运之四:“鸟巢”旁边的洋人求票者

这个德国人在一张纸板上写着“我需要票”四个中国字&#xff0c;四处探寻。 这个家伙显然是一个“黄牛党”&#xff0c;手里拿着一叠票&#xff0c;在吆喝。旁边的小伙子是专职翻译呢&#xff0c;还是想买票的呢。 买洋人的票更要检验一下真假。 “100块钱卖不卖&#xff1f;”…

搭建串口收发与存储双口RAM简易应用系统

搭建串口收发与存储双口RAM简易应用系统 为了实现通过串口发送数据到 FPGA 中, FPGA 接收到数据后将数据存储在双口 ram 的 一段连续空间中,当需要时,按下按键 S0,则 FPGA 将 RAM 中存储的数据通过串口发送出 去。先进行功能划分:串口接收模块、按键消抖模块、 RAM 模块、…

PIC32单片机harmony开发环境搭建并新建harmony工程

软件&#xff1a;maplab x 编译器&#xff1a;xc32 开发框架&#xff1a;harmony 建议安装时选择英文&#xff0c;此处省略上面三个软件安装过程。 安装完毕后&#xff0c;tools -> plugins -> available plugin中选择maplab harmony configurator下载安装。 (注&am…

看奥运之五:关于“鸟巢”、“水立方”的数据和内涵

将国家体育场设计成“鸟巢”的形状&#xff0c;是由瑞士的赫尔佐格与德梅隆建筑设计事务所提出的。中方的施工图设计单位是中国建筑设计院。总建筑师是李兴纲&#xff0c;纲结构设计师是范重。2006年&#xff0c;“鸟巢”被英国著名杂志《建筑新闻》列入令人惊异的世界十大建筑…

是谁压垮了刘翔?

是谁压垮了刘翔&#xff1f; 师傅。 跑道。 广告。 这张照片是在第10届全运会上拍摄的&#xff0c;是刘翔冲刺的一刹那。运动员身上所穿服装的标志&#xff0c;是日本货―美津浓&#xff08;全运会服装的唯一指定商&#xff09;。刘翔却用胶布把商标贴的死死的。 国家体育总…

PWM 发生器模块设计

PWM 发生器模块设计 PWM 产生原理可得出以下两个主要电路: 定时器/计数器电路以及输出比较电路。 最终输出 PWM 波的频率计算公式为:𝑓𝑝𝑤𝑚 =𝑓𝑐𝑙𝑘 / (𝑐𝑜𝑢𝑛𝑡𝑒𝑟_𝑎𝑟𝑟 + 1) 因此, 当输出频率确定时,可计算得到预重装值,…

在线IDE

http://www.sourcelair.com/ 支持多种语言

64位Ubuntu kylin 16.04 安装laptop mode解决关闭盖子无法唤醒,并解决安装此模式后鼠标间歇断电

建议以后安装分配交换空间,就不会碰到这种无法唤醒的鸟事了。 出处&#xff1a;http://blog.csdn.net/huludan/article/details/52725594 出处&#xff1a;http://www.tuicool.com/articles/uiMzuuI 解决办法&#xff1a;安装laptop-mode-tools工具包。 1.检查是否安装了grep…

Visual Basic团队透露将为VB添加迭代器

Visual Basic团队的Paul Vick最近泄露了一个计划——在一个还没有取名的版本中添加迭代器。虽然这意味着将要实现一个和C#的yield return类似的语句&#xff0c;但是这个计划看上去更像是函数式程序员常用的一种功能。\u0026#xD;\nC#的迭代器是一种延续的形式&#xff0c;在类似…

生活的花环:看雷加对文学的回顾

在《北京文学》工作时&#xff0c;经常在文联的春节联欢会上见到雷加先生&#xff0c;只是远远的看&#xff0c;没有说过话。但是知道他是延安时期的重要作家&#xff0c;新中国成立后也有不少作品问世&#xff0c;可惜我真的没有怎么读过他的作品。后来因为编辑他的新书《生活…

opendir 从不兼容的指针类型赋值

// open DIRs DIR *dir;struct dirent *dirp;if ((dpopendir(fullpath))NULL){write_log("No such dir:%s!\n",fullpath);return -1;}如果在dir 前面的DIR加一个struct 修饰符&#xff0c;opendir这里就有 从不兼容的指针类型赋值 警告。 t…

64位Ubuntu kylin 16.04使用fastboot下载内核到tiny4412开发板

1.编译内核&#xff0c;使用官方的linux-3.5-20151029.tgz 2.解压进入内核目录 3.cp tiny4412_linux_defconfig .config 4.make menuconfig System Type ---> [ ] Support TrustZone-enabled Trusted Execution Environment #去掉这个选项 Kernel hacking ---> …