Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Linux userspace tools for live migration
[palacios.git] / linux_usr / v3_mem_free.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 int main(void) 
6 {
7     FILE * fp;
8     char * filepath = "/proc/v3vee/v3-mem";
9     char line[255];
10     unsigned long base_addr = 0;
11     unsigned long long num_pages = 0;
12     unsigned long amount_allocated = 0;
13     unsigned long long block_size = 0;
14     int i = 0;
15     
16     fp = fopen(filepath, "r");
17     if(fp == NULL) {
18         fprintf(stderr, "Could not open %s\n", filepath);
19         return -1;
20     }
21     memset(line, 0, 255);
22     fgets(line, 255, fp);
23     base_addr = strtoul(line, NULL, 16);
24     base_addr = base_addr / (1024*1024);
25     memset(line, 0, 255);
26     fgets(line, 255, fp);
27     num_pages = strtoull(line, NULL, 10);
28     amount_allocated = (num_pages*4096)/(1024*1024);
29     
30     fclose(fp);
31     
32     /* now get the block size */
33     fp = fopen("/sys/devices/system/memory/block_size_bytes", "r");
34     if(fp == NULL) {
35         fprintf(stderr, "Cannot lookup bytes per block size\n");
36         return -1;
37     }
38     memset(line, 0, 255);
39     fgets(line, 255, fp);
40     block_size = strtoull(line, NULL, 16);
41     block_size = block_size / (1024*1024); //convert to MB
42     fclose(fp);
43     
44     /* turn base_addr into the region number */
45     base_addr = base_addr / block_size;
46     
47     for(i=0;i< (amount_allocated / block_size); i++) {
48         char path[50];
49         
50         memset(path, 0, 50);
51         sprintf(path,"/sys/devices/system/memory/memory%d/state", 
52                 base_addr);
53         fp = fopen(path, "w+");
54         if(fp == NULL) {
55             fprintf(stderr, "Could not open %s\n", path);
56             return -1;
57         }
58         printf("Sending \"online\" to memory%d\n", base_addr);
59         fprintf(fp, "online");
60         fclose(fp);
61         base_addr++;
62     }
63     
64     return 0;
65 }