-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove cgo Go can call POSIX API without using cgo. * update glide.yaml and glide.lock * miss * review changes * match the original C code.
- Loading branch information
1 parent
94afbc3
commit 275dfba
Showing
5 changed files
with
80 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,55 @@ | ||
package cachectl | ||
|
||
/* | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <sys/mman.h> | ||
int activePages(const char *path) | ||
{ | ||
int i, j, fd, pages, pagesize; | ||
struct stat st; | ||
void *m; | ||
char *pageinfo; | ||
fd = open(path, O_RDONLY); | ||
if(fd == -1) { | ||
return -1; | ||
} | ||
if(fstat(fd, &st) == -1) { | ||
goto error; | ||
} | ||
pagesize = getpagesize(); | ||
pages = (st.st_size + pagesize - 1) / pagesize; | ||
pageinfo = calloc(sizeof(*pageinfo), pages); | ||
if(!pageinfo) { | ||
goto error; | ||
} | ||
m = mmap(NULL, st.st_size, PROT_NONE, MAP_SHARED, fd, 0); | ||
if(m == MAP_FAILED) { | ||
free(pageinfo); | ||
goto error; | ||
} | ||
if(mincore(m, st.st_size, pageinfo) == -1) { | ||
free(pageinfo); | ||
munmap(m, st.st_size); | ||
goto error; | ||
} | ||
i = 0; | ||
j = 0; | ||
for (i = 0; i < pages; i++) { | ||
if(pageinfo[i] & 1) { | ||
j++; | ||
} | ||
} | ||
free(pageinfo); | ||
munmap(m, st.st_size); | ||
return j; | ||
error: | ||
close(fd); | ||
return -1; | ||
} | ||
*/ | ||
import "C" | ||
import ( | ||
"os" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func activePages(path string) int { | ||
cs := C.CString(path) | ||
result := C.activePages(cs) | ||
C.free(unsafe.Pointer(cs)) | ||
return int(result) | ||
func activePages(path string) (int, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer f.Close() | ||
|
||
fi, err := f.Stat() | ||
if err != nil { | ||
return 0, err | ||
} | ||
fsize := fi.Size() | ||
|
||
if fsize == 0 { | ||
return 0, nil | ||
} | ||
|
||
mmap, err := unix.Mmap(int(f.Fd()), 0, int(fsize), unix.PROT_NONE, unix.MAP_SHARED) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer unix.Munmap(mmap) | ||
|
||
pagesize := int64(os.Getpagesize()) | ||
pages := (fsize + pagesize - 1) / pagesize | ||
pageinfo := make([]byte, pages) | ||
|
||
mmapPtr := uintptr(unsafe.Pointer(&mmap[0])) | ||
sizePtr := uintptr(fsize) | ||
pageinfoPtr := uintptr(unsafe.Pointer(&pageinfo[0])) | ||
|
||
ret, _, err := unix.Syscall(unix.SYS_MINCORE, mmapPtr, sizePtr, pageinfoPtr) | ||
if ret != 0 { | ||
return 0, err | ||
} | ||
|
||
result := 0 | ||
|
||
for _, p := range pageinfo { | ||
if p&1 == 1 { | ||
result++ | ||
} | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters