1 /*
2  * Copyright 2009-2017 Citrix Ltd and other contributors
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include <stdlib.h>
16 
17 #include <libxl.h>
18 #include <libxl_utils.h>
19 #include <libxlutil.h>
20 
21 #include "xl.h"
22 #include "xl_utils.h"
23 #include "xl_parse.h"
24 
set_memory_max(uint32_t domid,const char * mem)25 static int set_memory_max(uint32_t domid, const char *mem)
26 {
27     int64_t memorykb;
28 
29     memorykb = parse_mem_size_kb(mem);
30     if (memorykb == -1) {
31         fprintf(stderr, "invalid memory size: %s\n", mem);
32         return EXIT_FAILURE;
33     }
34 
35     if (libxl_domain_setmaxmem(ctx, domid, memorykb)) {
36         fprintf(stderr, "cannot set domid %u static max memory to : %s\n", domid, mem);
37         return EXIT_FAILURE;
38     }
39 
40     return EXIT_SUCCESS;
41 }
42 
main_memmax(int argc,char ** argv)43 int main_memmax(int argc, char **argv)
44 {
45     uint32_t domid;
46     int opt = 0;
47     char *mem;
48 
49     SWITCH_FOREACH_OPT(opt, "", NULL, "mem-max", 2) {
50         /* No options */
51     }
52 
53     domid = find_domain(argv[optind]);
54     mem = argv[optind + 1];
55 
56     return set_memory_max(domid, mem);
57 }
58 
set_memory_target(uint32_t domid,const char * mem)59 static int set_memory_target(uint32_t domid, const char *mem)
60 {
61     int64_t memorykb;
62 
63     memorykb = parse_mem_size_kb(mem);
64     if (memorykb == -1)  {
65         fprintf(stderr, "invalid memory size: %s\n", mem);
66         return EXIT_FAILURE;
67     }
68 
69     if (libxl_set_memory_target(ctx, domid, memorykb, 0, /* enforce */ 1)) {
70         fprintf(stderr, "cannot set domid %u dynamic max memory to : %s\n", domid, mem);
71         return EXIT_FAILURE;
72     }
73 
74     return EXIT_SUCCESS;
75 }
76 
main_memset(int argc,char ** argv)77 int main_memset(int argc, char **argv)
78 {
79     uint32_t domid;
80     int opt = 0;
81     const char *mem;
82 
83     SWITCH_FOREACH_OPT(opt, "", NULL, "mem-set", 2) {
84         /* No options */
85     }
86 
87     domid = find_domain(argv[optind]);
88     mem = argv[optind + 1];
89 
90     return set_memory_target(domid, mem);
91 }
92 
sharing(const libxl_dominfo * info,int nb_domain)93 static void sharing(const libxl_dominfo *info, int nb_domain)
94 {
95     int i;
96 
97     printf("Name                                        ID   Mem Shared\n");
98 
99     for (i = 0; i < nb_domain; i++) {
100         char *domname;
101         unsigned shutdown_reason;
102         domname = libxl_domid_to_name(ctx, info[i].domid);
103         shutdown_reason = info[i].shutdown ? info[i].shutdown_reason : 0;
104         printf("%-40s %5d %5lu  %5lu\n",
105                 domname,
106                 info[i].domid,
107                 (unsigned long) ((info[i].current_memkb +
108                     info[i].outstanding_memkb) / 1024),
109                 (unsigned long) (info[i].shared_memkb / 1024));
110         free(domname);
111     }
112 }
113 
main_sharing(int argc,char ** argv)114 int main_sharing(int argc, char **argv)
115 {
116     int opt = 0;
117     libxl_dominfo info_buf;
118     libxl_dominfo *info, *info_free = NULL;
119     int nb_domain, rc;
120 
121     SWITCH_FOREACH_OPT(opt, "", NULL, "sharing", 0) {
122         /* No options */
123     }
124 
125     if (optind >= argc) {
126         info = libxl_list_domain(ctx, &nb_domain);
127         if (!info) {
128             fprintf(stderr, "libxl_list_domain failed.\n");
129             return EXIT_FAILURE;
130         }
131         info_free = info;
132     } else if (optind == argc-1) {
133         uint32_t domid = find_domain(argv[optind]);
134         rc = libxl_domain_info(ctx, &info_buf, domid);
135         if (rc == ERROR_DOMAIN_NOTFOUND) {
136             fprintf(stderr, "Error: Domain \'%s\' does not exist.\n",
137                 argv[optind]);
138             return EXIT_FAILURE;
139         }
140         if (rc) {
141             fprintf(stderr, "libxl_domain_info failed (code %d).\n", rc);
142             return EXIT_FAILURE;
143         }
144         info = &info_buf;
145         nb_domain = 1;
146     } else {
147         help("sharing");
148         return EXIT_FAILURE;
149     }
150 
151     sharing(info, nb_domain);
152 
153     if (info_free)
154         libxl_dominfo_list_free(info_free, nb_domain);
155     else
156         libxl_dominfo_dispose(info);
157 
158     return EXIT_SUCCESS;
159 }
160 
161 /*
162  * Local variables:
163  * mode: C
164  * c-basic-offset: 4
165  * indent-tabs-mode: nil
166  * End:
167  */
168