int libvirt_max_cpus(void) { if (g_conn == NULL) return -1; int max_cpus = virConnectGetMaxVcpus(g_conn, NULL); if (max_cpus < 0) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } return max_cpus; }
int main(int argc,char *argv[]){ virConnectPtr conn; int vcpus; conn = virConnectOpen("xen:///"); if(conn==NULL){ fprintf(stderr,"Failed to open connection to qeme:///system\n"); return 1; } vcpus = virConnectGetMaxVcpus(conn,NULL); fprintf(stdout,"Maxium support virtual CPUs:%d\n",vcpus); virConnectClose(conn); return 0; }
static PromiseResult CreateVirtDom(EvalContext *ctx, virConnectPtr vc, Attributes a, const Promise *pp) { int alloc_file = false; char *xml_file; const char *name; char defaultxml[CF_MAXVARSIZE]; virDomainPtr dom; int i; snprintf(defaultxml, CF_MAXVARSIZE - 1, "<domain type='test'>" " <name>%s</name>" " <memory>8388608</memory>" " <currentMemory>2097152</currentMemory>" " <vcpu>2</vcpu>" " <os>" " <type>hvm</type>" " </os>" "</domain>", pp->promiser); for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { if (CF_RUNNING[i] > 0) { dom = virDomainLookupByID(vc, CF_RUNNING[i]); name = virDomainGetName(dom); if (name && strcmp(name, pp->promiser) == 0) { cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Found a running environment called '%s' - promise kept", name); return PROMISE_RESULT_NOOP; } virDomainFree(dom); } } for (i = 0; CF_SUSPENDED[i] != NULL; i++) { if (strcmp(CF_SUSPENDED[i], pp->promiser) == 0) { Log(LOG_LEVEL_INFO, "Found an existing, but suspended, environment id = %s, called '%s'", CF_SUSPENDED[i], CF_SUSPENDED[i]); } } if(a.env.spec) { xml_file = xstrdup(a.env.spec); alloc_file = true; } else { Log(LOG_LEVEL_VERBOSE, "No spec file is promised, so reverting to default settings"); xml_file = defaultxml; } PromiseResult result = PROMISE_RESULT_NOOP; if ((dom = virDomainCreateXML(vc, xml_file, 0))) { cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Created a virtual domain '%s'", pp->promiser); result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE); if (a.env.cpus != CF_NOINT) { int maxcpus; if ((maxcpus = virConnectGetMaxVcpus(vc, virConnectGetType(vc))) == -1) { Log(LOG_LEVEL_VERBOSE, "Can't determine the available CPU resources"); } else { if (a.env.cpus > maxcpus) { Log(LOG_LEVEL_INFO, "The promise to allocate %d CPUs in domain '%s' cannot be kept - only %d exist on the host", a.env.cpus, pp->promiser, maxcpus); } else if (virDomainSetVcpus(dom, (unsigned int) a.env.cpus) == -1) { Log(LOG_LEVEL_INFO, "Unable to adjust CPU count to %d", a.env.cpus); } else { Log(LOG_LEVEL_INFO, "Verified that environment CPU count is now %d", a.env.cpus); } } } if (a.env.memory != CF_NOINT) { unsigned long maxmem; if ((maxmem = virDomainGetMaxMemory(dom)) == -1) { Log(LOG_LEVEL_VERBOSE, "Can't determine the available CPU resources"); } else { if (virDomainSetMaxMemory(dom, (unsigned long) a.env.memory) == -1) { Log(LOG_LEVEL_INFO, " Unable to set the memory limit to %d", a.env.memory); } else { Log(LOG_LEVEL_INFO, "Setting the memory limit to %d", a.env.memory); } if (virDomainSetMemory(dom, (unsigned long) a.env.memory) == -1) { Log(LOG_LEVEL_INFO, " Unable to set the current memory to %d", a.env.memory); } } } if (a.env.disk != CF_NOINT) { Log(LOG_LEVEL_VERBOSE, "Info: env_disk parameter is not currently supported on this platform"); } virDomainFree(dom); } else { virErrorPtr vp; vp = virGetLastError(); cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Failed to create a virtual domain '%s' - check spec for errors: '%s'", pp->promiser, vp->message); result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL); Log(LOG_LEVEL_VERBOSE, "Quoted spec file: %s", xml_file); } if (alloc_file) { free(xml_file); } return result; }
int main(int argvc, char *argv[]){ static virConnectPtr conn = NULL; static virDomainPtr dom = NULL; char * Sysinfo; char* Capabilities; char* Hostname; int Vcpunum ; const char* ConnectType; unsigned long * hvVer; int IsAlive; conn = virConnectOpen("xen:///"); if(conn == NULL){ fprintf(stderr,"Failed to open connection to xen"); return -1; } dom = virDomainLookupByID(conn,1); if(dom == NULL){ fprintf(stderr,"Failed to create a dom"); return -1; } Capabilities = virConnectGetCapabilities(conn);//get capabilities if(Capabilities == NULL){ fprintf(stdout,"failed to GetCapabilities"); return -1; } fprintf(stdout,"ConnectCapabilities %s\n",Capabilities); free(Capabilities); Sysinfo = virConnectGetSysinfo(conn,0);//Get Sysinfo if(Sysinfo ==NULL){ fprintf(stdout,"failed to getinfo"); return -1; } fprintf(stdout,"Sysinfo:%s\n",Sysinfo); free(Sysinfo); Hostname = virConnectGetHostname(conn);//get hostname if(Hostname == NULL){ fprintf(stderr,"failed to get hostname"); return -1; } fprintf(stdout,"Hostname:%s\n",Hostname); free(Hostname); Vcpunum = virConnectGetMaxVcpus(conn,"xen");//Get Vcpunum if(Vcpunum <0){ fprintf(stdout,"failed to GetCpunum"); return -1; } fprintf(stdout,"Vcpunum: %d\n",Vcpunum); ConnectType = virConnectGetType(conn);//Connect Type if(ConnectType ==NULL){ fprintf(stdout,"failed to GetConnectType"); return -1; } fprintf(stdout,"ConnectType is that :%s\n",ConnectType); if(virConnectGetVersion(conn,hvVer)<0){ fprintf(stderr,"Failed to Get Version"); return -1; } fprintf(stdout,"ConnectVersion: %lu\n",*hvVer); IsAlive = virConnectIsAlive(conn); if(IsAlive == 1 ){ fprintf(stdout,"alive\n"); } else if(IsAlive == 0){ fprintf(stdout,"dead\n"); } else fprintf(stdout,"Failed to get Connect Status\n"); free(dom); virConnectClose(conn); return 0; }
static int CreateVirtDom(virConnectPtr vc, char *uri, Attributes a, Promise *pp) { int alloc_file = false; char *xml_file; const char *name; char defaultxml[CF_MAXVARSIZE]; virDomainPtr dom; int i; snprintf(defaultxml, CF_MAXVARSIZE - 1, "<domain type='test'>" " <name>%s</name>" " <memory>8388608</memory>" " <currentMemory>2097152</currentMemory>" " <vcpu>2</vcpu>" " <os>" " <type>hvm</type>" " </os>" "</domain>", pp->promiser); for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { if (CF_RUNNING[i] > 0) { dom = virDomainLookupByID(vc, CF_RUNNING[i]); name = virDomainGetName(dom); if (name && strcmp(name, pp->promiser) == 0) { cfPS(cf_verbose, CF_NOP, "", pp, a, " -> Found a running environment called \"%s\" - promise kept\n", name); return true; } virDomainFree(dom); } } for (i = 0; CF_SUSPENDED[i] != NULL; i++) { if (strcmp(CF_SUSPENDED[i], pp->promiser) == 0) { CfOut(cf_inform, "", " -> Found an existing, but suspended, environment id = %s, called \"%s\"\n", CF_SUSPENDED[i], CF_SUSPENDED[i]); } } if(a.env.spec) { xml_file = xstrdup(a.env.spec); alloc_file = true; } else { CfOut(cf_verbose, "", "No spec file is promised, so reverting to default settings"); xml_file = defaultxml; } if ((dom = virDomainCreateXML(vc, xml_file, 0))) { cfPS(cf_verbose, CF_CHG, "", pp, a, " -> Created a virtual domain \"%s\"\n", pp->promiser); if (a.env.cpus != CF_NOINT) { int maxcpus; if ((maxcpus = virConnectGetMaxVcpus(vc, virConnectGetType(vc))) == -1) { CfOut(cf_verbose, "", " !! Can't determine the available CPU resources"); } else { if (a.env.cpus > maxcpus) { CfOut(cf_inform, "", " !! The promise to allocate %d CPUs in domain \"%s\" cannot be kept - only %d exist on the host", a.env.cpus, pp->promiser, maxcpus); } else if (virDomainSetVcpus(dom, (unsigned int) a.env.cpus) == -1) { CfOut(cf_inform, "", " -> Unable to adjust CPU count to %d", a.env.cpus); } else { CfOut(cf_inform, "", " -> Verified that environment CPU count is now %d", a.env.cpus); } } } if (a.env.memory != CF_NOINT) { unsigned long maxmem; if ((maxmem = virDomainGetMaxMemory(dom)) == -1) { CfOut(cf_verbose, "", " !! Can't determine the available CPU resources"); } else { if (virDomainSetMaxMemory(dom, (unsigned long) a.env.memory) == -1) { CfOut(cf_inform, "", " !!! Unable to set the memory limit to %d", a.env.memory); } else { CfOut(cf_inform, "", " -> Setting the memory limit to %d", a.env.memory); } if (virDomainSetMemory(dom, (unsigned long) a.env.memory) == -1) { CfOut(cf_inform, "", " !!! Unable to set the current memory to %d", a.env.memory); } } } if (a.env.disk != CF_NOINT) { CfOut(cf_verbose, "", " -> Info: env_disk parameter is not currently supported on this platform"); } virDomainFree(dom); } else { virErrorPtr vp; vp = virGetLastError(); cfPS(cf_verbose, CF_FAIL, "", pp, a, " !! Failed to create a virtual domain \"%s\" - check spec for errors: %s", pp->promiser, vp->message); CfOut(cf_verbose, "", "Quoted spec file: %s", xml_file); } if (alloc_file) { free(xml_file); } return true; }
int main() { int idCount; int i; int id; //int ids[MAXID]; int *ids; //timeInfoNode timeInfos[MAXID]; printf("--------------------------------------------------------\n"); printf(" XEN Domain Monitor \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; } /*char* caps; caps = virConnectGetCapabilities(conn); printf("Capabilities:\n%s\n",caps); free(caps);*/ char *host; host = virConnectGetHostname(conn); fprintf(stdout, "Hostname:%s\n",host); free(host); int vcpus; vcpus = virConnectGetMaxVcpus(conn,NULL); fprintf(stdout, "Maxmum support vcpus:%d\n",vcpus); unsigned long long node_free_memory; node_free_memory = virNodeGetFreeMemory(conn); fprintf(stdout, "free memory:%lld\n",node_free_memory); virNodeInfo nodeinfo; virNodeGetInfo(conn,&nodeinfo); fprintf(stdout, "Model: %s\n", nodeinfo.model); fprintf(stdout, "Memory size: %lukb\n", nodeinfo.memory); fprintf(stdout, "Number of CPUs: %u\n", nodeinfo.cpus); fprintf(stdout, "MHz of CPUs: %u\n", nodeinfo.mhz); fprintf(stdout, "Number of NUMA nodes: %u\n", nodeinfo.nodes); fprintf(stdout, "Number of CPU sockets: %u\n", nodeinfo.sockets); fprintf(stdout, "Number of CPU cores per socket: %u\n", nodeinfo.cores); fprintf(stdout, "Number of CPU threads per core: %u\n", nodeinfo.threads); fprintf(stdout, "Virtualization type: %s\n", virConnectGetType(conn)); unsigned long ver; virConnectGetVersion(conn, &ver); fprintf(stdout, "Version: %lu\n", ver); /*unsigned long Libver; virConnectGetLibVersion(conn, &Libver); fprintf(stdout, "Libvirt Version: %lu\n", Libver);*/ char *uri; uri = virConnectGetURI(conn); fprintf(stdout, "Canonical URI: %s\n", uri); free(uri); /* get the count of IDs and save these ID into ids[] */ idCount = virConnectNumOfDomains(conn); ids = malloc(sizeof(int) *idCount); idCount = virConnectListDomains(conn,ids,idCount); //idCount = virConnectListDomains(conn, &ids[0], MAXID); if (idCount < 0) { fprintf(stderr, "Failed to list the domains\n"); closeConn(); return 0; } timeInfoNode timeInfos[idCount]; 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); /* loop print the domain info and calculate the usage of cpus*/ for (i = 0; i < idCount; i++) { id = ids[i]; getDomainInfo(id, timeInfos[i]); } free(ids); printf("--------------------------------------------------------\n"); closeConn(); return 0; }