📄 control.c
字号:
/* * control.c * * 2006 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru> * All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <sys/poll.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <fcntl.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <ctype.h>#include "include/linux/types.h"#include "net/core/alloc/avl.h"#include "control.h"static int zc_mmap(struct zc_control *ctl, struct zc_status *st){ struct zc_data *e; unsigned int entry_num = st->entry_num; int i; for (i=0; i<ZC_MAX_ENTRY_NUM; ++i) { struct zc_entry_status *ent = &st->entry[i]; e = &ctl->e[i]; if (e->data.ptr || !ent->node_num) continue; e->data.ptr = mmap(NULL, PAGE_SIZE*(1<<ent->node_order)*ent->node_num, PROT_READ|PROT_WRITE, MAP_SHARED, ctl->fd, ctl->offset*PAGE_SIZE); if (e->data.ptr == MAP_FAILED) { fprintf(stderr, "Failed to mmap: number: %u, order: %u, offset: %u, errno: %d - %s.\n", ent->node_num, ent->node_order, ctl->offset, errno, strerror(errno)); e->data.ptr = NULL; return -1; } printf("mmap: %2d.%2d: cpu: %d, ptr: %p, number: %u, order: %u, offset: %u.\n", i, st->entry_num, ctl->cpu, e->data.ptr, ent->node_num, ent->node_order, ctl->offset); ctl->offset += (1<<ent->node_order)*ent->node_num; e->entry = i; if (--entry_num == 0) break; } return 0;}int zc_prepare(struct zc_control *ctl, unsigned int entry_num){ struct zc_data *e; void *ptr; e = &ctl->e[entry_num]; ptr = e->data.ptr; if (!ptr) { int err; struct zc_status st; memset(&st, 0, sizeof(struct zc_status)); st.entry_num = 0; err = ioctl(ctl->fd, ZC_STATUS, &st); if (err) { fprintf(stderr, "Failed to get status for CPU%d: %s [%d].\n", ctl->cpu, strerror(errno), errno); return err; } err = zc_mmap(ctl, &st); if (err) return err; } return 0;}struct zc_control *zc_ctl_init(int nr_cpus, char *ctl_file){ int i, err; struct zc_control *zc_ctl, *ctl; zc_ctl = malloc(nr_cpus * sizeof(struct zc_control)); if (!zc_ctl) { fprintf(stderr, "Failed to allocate control structures for %d cpus.\n", nr_cpus); return NULL; } memset(zc_ctl, 0, nr_cpus * sizeof(struct zc_control)); for (i=0; i<nr_cpus; ++i) { ctl = &zc_ctl[i]; ctl->cpu = i; ctl->fd = open(ctl_file, O_RDWR); if (!ctl->fd) { fprintf(stderr, "Failed to open control file %s: %s [%d].\n", ctl_file, strerror(errno), errno); return NULL; } err = ioctl(ctl->fd, ZC_SET_CPU, &ctl->cpu); if (err) { close(ctl->fd); fprintf(stderr, "Failed to setup CPU%d.\n", ctl->cpu); return NULL; } } return zc_ctl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -