ntubski |
02-06-2011 08:51 PM |
So it turns out increasing the buffer size doesn't help, I rewrote lc.c to use open() and read(), and discovered that it doesn't read() more than 4096 bytes at a time.
Code:
~/tmp$ ./lc /proc/net/ip_conntrack
nread = 4080, BUFFER_SIZE = 4194304
nread = 4080, BUFFER_SIZE = 4194304
nread = 4080, BUFFER_SIZE = 4194304
nread = 3060, BUFFER_SIZE = 4194304
76 /proc/net/ip_conntrack
Code:
/* Sample implementation of wc utility. */
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef unsigned long count_t; /* Counter type */
static char *buffer;
/* make this a bit bigger than the size of the file */
#define BUFFER_SIZE (4*1024*1024) /* 3 891 335 */
/* Current file counters: chars, words, lines */
count_t ccount;
count_t wcount;
count_t lcount;
/* Totals counters: chars, words, lines */
count_t total_ccount = 0;
count_t total_wcount = 0;
count_t total_lcount = 0;
/* Print error message and exit with error status. If PERR is not 0,
display current errno status. */
static void
error_print (int perr, char *fmt, va_list ap)
{
vfprintf (stderr, fmt, ap);
if (perr)
perror (" ");
else
fprintf (stderr, "\n");
exit (1);
}
/* Print error message and exit with error status. */
static void
errf (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
error_print (0, fmt, ap);
va_end (ap);
}
/* Print error message followed by errno status and exit
with error code. */
static void
perrf (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
error_print (1, fmt, ap);
va_end (ap);
}
/* Output counters for given file */
void
report (const char *file, count_t lcount)
{
printf ("%6lu %s\n", lcount, file);
}
/* Process file FILE. */
void
counter (const char *file)
{
int fd = open (file, O_RDONLY);
int total_read = 0, lcount = 0;
if (fd < 0)
perrf ("cannot open file `%s'", file);
for (;;) {
int nread = read(fd, buffer+total_read, BUFFER_SIZE - total_read);
if (nread < 0) perrf("read");
if (nread == 0) break; /* eof */
total_read += nread;
/* printf("nread = %d, BUFFER_SIZE = %d\n", nread, BUFFER_SIZE); */
}
for (const char *p = buffer; p; p = memchr(p+1, '\n', total_read - (p+1 - buffer)))
lcount++;
report (file, lcount);
total_lcount += lcount;
close(fd);
}
int
main (int argc, char **argv)
{
int i;
if (argc < 2)
errf ("usage: lc FILE [FILE...]");
buffer = malloc(BUFFER_SIZE);
for (i = 1; i < argc; i++)
counter (argv[i]);
if (argc > 2)
report ("total", total_lcount);
return 0;
}
The program to open a bunch of ports, if anyone is interested:
Code:
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/epoll.h>
int client_socket(int port)
{
int status, yes = 1, fail;
struct addrinfo hints;
struct addrinfo *servinfo, *si; // will point to the results
char portname[8];
int sockfd;
snprintf(portname, sizeof portname, "%d", port);
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_NUMERICSERV; /* numeric address */
if ((status = getaddrinfo("127.0.0.1", portname, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
// servinfo now points to a linked list of 1 or more struct addrinfos
for (si = servinfo; si; si = si->ai_next) {
sockfd = socket(si->ai_family, si->ai_socktype, si->ai_protocol);
if (sockfd < 0) {
perror("socket");
continue;
}
fail = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
if (fail) { perror("fail"); exit(1); }
break;
}
if (!si) {
exit(1);
}
freeaddrinfo(servinfo); // free the linked-list
fail = connect(sockfd, si->ai_addr, si->ai_addrlen);
if (fail) { perror("connect"); exit(1); }
return sockfd;
}
int server_socket(int port)
{
int status, yes = 1, fail;
struct addrinfo hints;
struct addrinfo *servinfo, *si; // will point to the results
char portname[8];
int sockfd;
snprintf(portname, sizeof portname, "%d", port);
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
if ((status = getaddrinfo(NULL, portname, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
// servinfo now points to a linked list of 1 or more struct addrinfos
for (si = servinfo; si; si = si->ai_next) {
sockfd = socket(si->ai_family, si->ai_socktype, si->ai_protocol);
if (sockfd < 0) continue;
fail = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
if (fail) { perror("fail"); exit(1); }
fail = bind(sockfd, si->ai_addr, si->ai_addrlen);
if (!fail) break;
else close(sockfd);
}
if (!si) {
perror("bind");
exit(1);
}
freeaddrinfo(servinfo); // free the linked-list
fail = listen(sockfd, 1);
if (fail) {
perror("listen");
exit(1);
}
return sockfd;
}
int main(int argc, char **argv)
{
assert(argc == 4);
int portlo = atoi(argv[2]);
int porthi = atoi(argv[3]);
assert(portlo && porthi);
if (strcmp("--client", argv[1]) == 0) {
for (int p = portlo; p < porthi; p++) {
client_socket(p);
if (p % 100 == 0) printf(".");
if (p % 1000 == 0) printf("%d\n", p);
}
goto hang;
} /* else serve */
int epollfd;
#define MAX_EVENTS 1024
static struct epoll_event ev, events[MAX_EVENTS];
epollfd = epoll_create(porthi - portlo);
if (epollfd < 0) { perror("epoll_create"); exit(1); }
ev.events = EPOLLIN;
for (int p = portlo; p < porthi; p++) {
int sock = server_socket(p);
ev.data.fd = sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) {
perror("epoll_ctl(add, listener)");
exit(1);
}
if (p % 100 == 0) printf(".");
if (p % 1000 == 0) printf("%d\n", p);
}
for (;;) {
int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
if (nfds < 0) { perror("epoll_wait"); exit(1); }
for (int n = 0; n < nfds; n++ ) {
int tcpsock = accept(events[n].data.fd, NULL, NULL);
if (tcpsock < 0) {
perror("accept"); exit(1);
}
if (epoll_ctl(epollfd, EPOLL_CTL_DEL, events[n].data.fd, &events[n]) == -1) {
perror("epoll_ctl(del, listener)"); exit(1);
}
close(events[n].data.fd); /* done listening */
}
}
hang:
for (;;) sleep(1);
return 0;
}
|