int virConnectClose (virConnectPtr conn)
释放hyperbisor的连接,如果程序出线异常或错误应调用本函数终止连接 conn
hypervisor的指针 Returns
成功释放返回0,否则返回-1
int virDomainFree (virDomainPtr domain)
释放域的指针,如果连接到域就要先调用这个函数释放域的链接,之后再释放hyperbisor的连接 domain
域的指针 Returns
成功释放返回0,否则返回-1
一个主要的数据结构:
struct virDomainInfo unsigned char
state
当前域的运行状态 unsigned long
maxMem
支持的最大内存 unsigned long
memory
使用的内存 unsigned short
nrVirtCpu
虚拟CPU数量 unsigned long long
cpuTime
虚拟CPU运行时间
/**
* Project: myxm
* Version: 0.2
* Abstract: A simple xen monitor
* Author: Gu Xiangnan
* Date: 2008-05-25
*/
#include
#include
#include
#define MAXID 50
/* the data structure of time */
typedef struct timeInfo
{
long long cpu_time;
struct timeval real_time;
} timeInfoNode;
/* the hypervisor connection */
static virConnectPtr conn = NULL;
/* release the connect of hypervisor */
void closeConn()
{
if (conn != NULL)
virConnectClose(conn);
}
/* release the domain pointer */
void freeDom(virDomainPtr dom)
{
if (dom != NULL)
virDomainFree(dom);
}
/* get the 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 (dom == NULL)
{
fprintf(stderr, "Failed to find Domain %d/n", 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/n", id);
freeDom(dom);
closeConn();
}
/* get the start of realTime*/
if (gettimeofday(&(infos->real_time), NULL) == - 1)
{
fprintf(stderr, "Failed to get start time/n");
return;
}
/* get the start of CPUTime*/
infos->cpu_time = info.cpuTime; /* nanosecond */
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 (dom == NULL)
{
fprintf(stderr, "Failed to find Domain %d/n", 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/n", id);
freeDom(dom);
closeConn();
}
/* get the end of realTime*/
if (gettimeofday(&realTime, NULL) == - 1)
{
fprintf(stderr, "Failed to get start time/n");
return;
}
/* calculate the usage of cpu */
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);
/* print the results */
printf("%d/t%.3f%/t%lu/t%lu/t%hu/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];
timeInfoNode timeInfos[MAXID];
printf("--------------------------------------------------------/n");
printf(" XEN Domain Monitor Version 0.2/n");
printf(" Build by Gu Xiangnan 35060514/n");
printf("--------------------------------------------------------/n");
/* NULL means connect to local Xen hypervisor */
conn = virConnectOpenReadOnly(NULL);
if (conn == NULL)
{
fprintf(stderr, "Failed to connect to hypervisor/n");
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/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;
getTimeInfo(id, &(timeInfos));
}
sleep(1);
/* loop print the domain info and calculate the usage of cpus*/
for (i = 0; i < idCount; i++)
{
id = ids;
getDomainInfo(id, timeInfos);
}
printf("--------------------------------------------------------/n");
closeConn();
return 0;
}