已解决
Linux高性能服务器编程——ch16笔记
来自网友在路上 164864提问 提问时间:2023-10-31 19:47:19阅读次数: 64
最佳答案 问答题库648位专家为你答疑解惑
第16章 服务器调制、调试和测试
16.1 最大文件描述符
作为守护进程运行的服务器程序就应该总是关闭标准输入、标准输出和标准错误这3个文件描述符。
两个层次的限制:用户级限制和系统级限制。
16.2 调整内核参数
文件系统:/proc/sys/fs下。
网络模块:/proc/sys/net下。
16.3 gdb调试
调试多进程程序:
1)单独调试子进程:找到目标子进程的PID,再attach到调试器上。
2)使用调试器选项follow-fork-mode:允许选择在执行fork后是继续调试父进程还是子进程。
调试多线程程序:
1)info threads:显示当前可调试的所有线程。
2)thread ID:调试目标ID指定的线程。
3)set scheduler-locking [off|on|step]:不锁定任何线程|当前被调试的线程会继续执行|单步执行时,只有当前线程会执行。
16.4 压力测试
单纯的I/O复用方式的施压程度是最高的,因为线程和进程的调度本身也是要占用一定CPU时间的。
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>/* 每个客户连接不停的向服务器发送这个请求 */
static const char *request = "GET http://localhost/index.html HTTP/1.1\r\nConnection: keep-alive\r\n\r\nxxxxxxxxxxxxxxxxxxxxxx";int setnonblokcing(int fd)
{int old_option = fcntl(fd, F_GETFL);int new_option = old_option | O_NONBLOCK;fcntl(fd, F_SETFL, new_option);return old_option;
}void addfd(int epoll_fd, int fd)
{epoll_event event;event.data.fd = fd;event.events = EPOLLOUT | EPOLLET | EPOLLERR;epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event);setnonblokcing(fd);
}/* 向服务器写入len字节的数据 */
bool write_nbytes(int sockfd, const char *buffer, int len)
{int bytes_write = 0;printf("write out %d bytes to socket %d\n", len, sockfd);while (1){bytes_write = send(sockfd, buffer, len, 0);if (bytes_write == -1){return false;}else if (bytes_write == 0){return false;}len -= bytes_write;buffer = buffer + bytes_write;if (len <= 0){return true;}}
}/* 从服务器读取数据 */
bool read_once(int sockfd, char *buffer, int len)
{int bytes_read = 0;memset(buffer, '\0', len);bytes_read = recv(sockfd, buffer, len, 0);if (bytes_read == -1){return false;}else if (bytes_read == 0){return false;}printf("read in %d bytes from socket %d with content: %s\n", bytes_read, sockfd, buffer);return true;
}/* 向服务器发起num个TCP连接,我们可以通过改变num来调整测试压力 */
void start_conn(int epoll_fd, int num, const char *ip, int port)
{struct sockaddr_in address;bzero(&address, sizeof(address));address.sin_family = AF_INET;inet_pton(AF_INET, ip, &address.sin_addr);address.sin_port = htons(port);for (int i = 0; i < num; ++i){sleep(1);int sockfd = socket(PF_INET, SOCK_STREAM, 0);printf("create %d sock\n", i);if (sockfd < 0);{continue;}if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == 0){printf("build connection %d\n", i);addfd(epoll_fd, sockfd);}}
}void close_conn(int epoll_fd, int sockfd)
{epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sockfd, 0);close(sockfd);
}int main(int argc, char *argv[])
{if (argc != 4){printf("usage: %s ip_address, port_number client_num\n", basename(argv[0]));return 1;}int epoll_fd = epoll_create(100);start_conn(epoll_fd, atoi(argv[3]), argv[1], atoi(argv[2]));epoll_event events[10000];char buffer[2048];while (1){int fds = epoll_wait(epoll_fd, events, 10000, 2000);for (int i = 0; i < fds; ++i){int sockfd = events[i].data.fd;if (events[i].events & EPOLLIN){if (!read_once(sockfd, buffer, 2048)){close_conn(epoll_fd, sockfd);}struct epoll_event event;event.events = EPOLLOUT | EPOLLET | EPOLLERR;event.data.fd = sockfd;epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);}else if (events[i].events & EPOLLOUT){if (!write_nbytes(sockfd, request, strlen(request))){close_conn(epoll_fd, sockfd);}struct epoll_event event;event.events = EPOLLIN | EPOLLET | EPOLLERR;event.data.fd = sockfd;epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);}else if (events[i].events & EPOLLERR){close_conn(epoll_fd, sockfd);}}}
}
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"Linux高性能服务器编程——ch16笔记":http://eshow365.cn/6-28938-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!