#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
bool flag =true;
char pagemap_path[100];
const int a1 = 100;
int a2 = 200;
void mem_addr(unsigned long vaddr, unsigned long *v_index,unsigned long *paddr,unsigned long *p_index)
{
unsigned long pageSize =(unsigned long)getpagesize();
unsigned long v_pageIndex = vaddr / pageSize;
*v_index = v_pageIndex;
uint64_t v_offset = v_pageIndex * sizeof(uint64_t);
unsigned long page_offset = vaddr % pageSize;
uint64_t item = 0;
int fd = open(pagemap_path, O_RDONLY);
if(fd == -1)
{
printf("open /proc/self/pagemap error\n");
flag = false;
return;
}
if(lseek(fd, v_offset, SEEK_SET) == -1)
{
printf("sleek error\n");
flag = false;
return;
}
if(read(fd, &item, sizeof(uint64_t)) != sizeof(uint64_t))
{
printf("read item error\n");
flag = false;
return;
}
if((((uint64_t)1 << 63) & item) == 0)
{ printf("page present is 0\n");
flag = false;
return ;
}
unsigned long phy_pageIndex = (((uint64_t)1 << 55) - 1) & item;
*p_index = phy_pageIndex;
*paddr = (phy_pageIndex * pageSize) + page_offset;
close(fd);
}
int main()
{
int b1 = 100;
int b2 = 100;
static int c1 = 100;
static int c2 = 100;
const int d1 = 100;
const int d2 =100;
unsigned long phy = 0;
unsigned long v_index = 0;
unsigned long p_index = 0;
double *p1 = (double*)malloc(10000*sizeof(double));
double *p2 = (double*)malloc(10000*sizeof(double));
int g = getpid();
char buf[10];
sprintf(buf,"%d",g);
strcat(pagemap_path,"/proc/");
strcat(pagemap_path,buf);
strcat(pagemap_path,"/pagemap");
printf("当前进程id:%d\n",getpid());
mem_addr((unsigned long )&b2, &v_index, &phy, &p_index);
mem_addr((unsigned long )&a1, &v_index, &phy, &p_index);
printf("全局常量a1:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n",(unsigned long)&a1, v_index, phy, p_index);
mem_addr((unsigned long)&a2, &v_index, &phy, &p_index);
printf("全局变量a2:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n",(unsigned long)&a2, v_index, phy, p_index);
mem_addr((unsigned long)&b1, &v_index, &phy, &p_index);
printf("局部变量b1:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long)&b1, v_index, phy, p_index);
mem_addr((unsigned long)&b2, &v_index, &phy, &p_index);
printf("局部变量b2:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long )&b2, v_index, phy, p_index);
mem_addr((unsigned long)&c1, &v_index, &phy, &p_index);
printf("局部静态变量c1:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long)&c1, v_index, phy, p_index);
mem_addr((unsigned long)&c2, &v_index, &phy, &p_index);
printf("局部静态变量c2:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long)&c2, v_index, phy, p_index);
mem_addr((unsigned long )&d1, &v_index, &phy, &p_index);
printf("局部常量d1:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long)&d1, v_index, phy, p_index);
mem_addr((unsigned long)&d2, &v_index, &phy, &p_index);
printf("局部常量d2:virtual addr = %lu , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long)&d2, v_index, phy, p_index);
mem_addr((unsigned long)p1, &v_index, &phy, &p_index);
printf("动态内存p1:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long)p1, v_index, phy, p_index);
mem_addr((unsigned long)p2, &v_index, &phy, &p_index);
printf("动态内存p2:virtual addr = %lx , virtual index = %lx , physical addr = %lx , physical index = %lx\n", (unsigned long)p2, v_index, phy, p_index);
free(p1);
free(p2);
sleep(100000);
return 0;
}