Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/posixshmcontrol/posixshmcontrol.c
Show All 24 Lines | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/filio.h> | |||||
#include <sys/mman.h> | #include <sys/mman.h> | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <sys/syscall.h> | |||||
#include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
#include <sys/user.h> | #include <sys/user.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <grp.h> | #include <grp.h> | ||||
#include <libutil.h> | #include <libutil.h> | ||||
#include <pwd.h> | #include <pwd.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
static void | static void | ||||
usage(void) | usage(void) | ||||
{ | { | ||||
fprintf(stderr, "Usage:\n" | fprintf(stderr, "Usage:\n" | ||||
"posixshmcontrol create [-m <mode>] <path> ...\n" | "posixshmcontrol create [-m <mode>] [-l largepage] <path> ...\n" | ||||
"posixshmcontrol rm <path> ...\n" | "posixshmcontrol rm <path> ...\n" | ||||
"posixshmcontrol ls [-h] [-n]\n" | "posixshmcontrol ls [-h] [-n]\n" | ||||
"posixshmcontrol dump <path> ...\n" | "posixshmcontrol dump <path> ...\n" | ||||
"posixshmcontrol stat [-h] [-n] <path> ...\n" | "posixshmcontrol stat [-h] [-n] <path> ...\n" | ||||
"posixshmcontrol truncate [-s <newlen>] <path> ...\n"); | "posixshmcontrol truncate [-s <newlen>] <path> ...\n"); | ||||
} | } | ||||
static int | static int | ||||
create_one_shm(const char *path, long mode) | create_one_shm(const char *path, long mode, int idx) | ||||
{ | { | ||||
int fd; | struct shm_largepage_conf slc; | ||||
int error, fd; | |||||
if (idx == -1) { | |||||
fd = shm_open(path, O_RDWR | O_CREAT, mode); | fd = shm_open(path, O_RDWR | O_CREAT, mode); | ||||
if (fd == -1) { | if (fd == -1) { | ||||
warn("create %s", path); | warn("create %s", path); | ||||
return (1); | return (1); | ||||
} | } | ||||
} else { | |||||
fd = syscall(SYS_shm_open2, path, O_CREAT | O_RDWR, 0666, | |||||
SHM_LARGEPAGE, (void *)NULL); | |||||
if (fd == -1) | |||||
warn("shm_open2 %s", path); | |||||
memset(&slc, 0, sizeof(slc)); | |||||
slc.psind = idx; | |||||
slc.alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT; | |||||
error = ioctl(fd, FIOSHMLPGCNF, &slc); | |||||
markj: This is still using the old ioctl. I guess it can use shm_create_largepage() now. | |||||
if (error == -1) | |||||
err(1, "FIOSHMLPGCNF"); | |||||
} | |||||
close(fd); | close(fd); | ||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
create_shm(int argc, char **argv) | create_shm(int argc, char **argv) | ||||
{ | { | ||||
char *end; | char *end; | ||||
size_t *pagesizes; | |||||
long mode; | long mode; | ||||
int c, i, ret, ret1; | uint64_t pgsz; | ||||
int c, i, idx, pn, ret, ret1; | |||||
bool printed; | |||||
mode = 0600; | mode = 0600; | ||||
while ((c = getopt(argc, argv, "m:")) != -1) { | idx = -1; | ||||
while ((c = getopt(argc, argv, "l:m:")) != -1) { | |||||
switch (c) { | switch (c) { | ||||
case 'm': | case 'm': | ||||
errno = 0; | errno = 0; | ||||
mode = strtol(optarg, &end, 0); | mode = strtol(optarg, &end, 0); | ||||
if (mode == 0 && errno != 0) | if (mode == 0 && errno != 0) | ||||
err(1, "mode:"); | err(1, "mode:"); | ||||
if (*end != '\0') | if (*end != '\0') | ||||
errx(1, "non-integer mode"); | errx(1, "non-integer mode"); | ||||
break; | break; | ||||
case 'l': | |||||
if (expand_number(optarg, &pgsz) == -1) | |||||
err(1, "size:"); | |||||
Done Inline ActionsExtra ":", ditto above with "mode:". markj: Extra ":", ditto above with "mode:". | |||||
pn = getpagesizes(NULL, 0); | |||||
if (pn == -1) | |||||
err(1, "getpagesizes"); | |||||
pagesizes = malloc(sizeof(size_t) * pn); | |||||
if (pagesizes == NULL) | |||||
err(1, "malloc"); | |||||
if (getpagesizes(pagesizes, pn) == -1) | |||||
err(1, "gtpagesizes"); | |||||
for (idx = 0; idx < pn; idx++) { | |||||
if (pagesizes[idx] == pgsz) | |||||
break; | |||||
} | |||||
if (idx == pn) { | |||||
fprintf(stderr, | |||||
"pagesize should be superpagesize, supported sizes:"); | |||||
printed = false; | |||||
for (i = 0; i < pn; i++) { | |||||
if (pagesizes[i] == 0 || | |||||
pagesizes[i] == (size_t) | |||||
getpagesize()) | |||||
continue; | |||||
printed = true; | |||||
fprintf(stderr, " %zu", pagesizes[i]); | |||||
} | |||||
if (!printed) | |||||
fprintf(stderr, " none"); | |||||
fprintf(stderr, "\n"); | |||||
exit(1); | |||||
} | |||||
if (pgsz == (uint64_t)getpagesize()) | |||||
errx(1, "pagesize should be large"); | |||||
free(pagesizes); | |||||
break; | |||||
case '?': | case '?': | ||||
default: | default: | ||||
usage(); | usage(); | ||||
return (2); | return (2); | ||||
} | } | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
ret = 0; | ret = 0; | ||||
for (i = 0; i < argc; i++) { | for (i = 0; i < argc; i++) { | ||||
ret1 = create_one_shm(argv[i], mode); | ret1 = create_one_shm(argv[i], mode, idx); | ||||
if (ret1 != 0 && ret == 0) | if (ret1 != 0 && ret == 0) | ||||
ret = ret1; | ret = ret1; | ||||
} | } | ||||
return (ret); | return (ret); | ||||
} | } | ||||
static int | static int | ||||
delete_one_shm(const char *path) | delete_one_shm(const char *path) | ||||
▲ Show 20 Lines • Show All 231 Lines • ▼ Show 20 Lines | if (error == -1) { | ||||
printf("atime\t%ld.%09ld\n", (long)st.st_atime, | printf("atime\t%ld.%09ld\n", (long)st.st_atime, | ||||
(long)st.st_atim.tv_nsec); | (long)st.st_atim.tv_nsec); | ||||
printf("mtime\t%ld.%09ld\n", (long)st.st_mtime, | printf("mtime\t%ld.%09ld\n", (long)st.st_mtime, | ||||
(long)st.st_mtim.tv_nsec); | (long)st.st_mtim.tv_nsec); | ||||
printf("ctime\t%ld.%09ld\n", (long)st.st_ctime, | printf("ctime\t%ld.%09ld\n", (long)st.st_ctime, | ||||
(long)st.st_ctim.tv_nsec); | (long)st.st_ctim.tv_nsec); | ||||
printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec, | printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec, | ||||
(long)st.st_birthtim.tv_nsec); | (long)st.st_birthtim.tv_nsec); | ||||
if (st.st_blocks != 0) | |||||
printf("pagesz\t%jd\n", roundup((uintmax_t)st.st_size, | |||||
PAGE_SIZE) / st.st_blocks); | |||||
} | } | ||||
close(fd); | close(fd); | ||||
return (ret); | return (ret); | ||||
} | } | ||||
static int | static int | ||||
stat_shm(int argc, char **argv) | stat_shm(int argc, char **argv) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines |
This is still using the old ioctl. I guess it can use shm_create_largepage() now.