| [0aec496] | 1 | #define __USE_GNU
|
|---|
| 2 |
|
|---|
| 3 | #include <errno.h>
|
|---|
| 4 | #include <stdio.h>
|
|---|
| 5 | #include <string.h>
|
|---|
| 6 | #include <unistd.h>
|
|---|
| 7 | extern "C" {
|
|---|
| 8 | #include <sys/socket.h>
|
|---|
| 9 | #include <netinet/in.h>
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | #include <kernel.hfa>
|
|---|
| 13 | #include <stats.hfa>
|
|---|
| 14 | #include <thread.hfa>
|
|---|
| 15 |
|
|---|
| 16 | #include "channel.hfa"
|
|---|
| 17 | #include "filecache.hfa"
|
|---|
| 18 | #include "options.hfa"
|
|---|
| [7f389a5c] | 19 | #include "parseargs.hfa"
|
|---|
| [0aec496] | 20 | #include "worker.hfa"
|
|---|
| 21 |
|
|---|
| 22 | //=============================================================================================
|
|---|
| 23 | // Globals
|
|---|
| 24 | //=============================================================================================
|
|---|
| 25 | Options options @= {
|
|---|
| 26 | 0,
|
|---|
| 27 | 42u,
|
|---|
| 28 | 0,
|
|---|
| 29 | false,
|
|---|
| 30 | false,
|
|---|
| 31 | 0
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | channel & wait_connect;
|
|---|
| 35 |
|
|---|
| 36 | struct ServerProc {
|
|---|
| 37 | processor self;
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | void ?{}( ServerProc & this ) {
|
|---|
| 41 | /* paranoid */ assert( options.the_cluster != 0p );
|
|---|
| 42 | (this.self){ "Benchmark Processor", *options.the_cluster };
|
|---|
| 43 |
|
|---|
| 44 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 45 | if( options.procstats ) {
|
|---|
| 46 | print_stats_at_exit( this.self, options.the_cluster->print_stats );
|
|---|
| 47 | }
|
|---|
| 48 | if( options.viewhalts ) {
|
|---|
| 49 | print_halts( this.self );
|
|---|
| 50 | }
|
|---|
| 51 | #endif
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | //=============================================================================================
|
|---|
| 55 | // Main
|
|---|
| 56 | //============================================================================================='
|
|---|
| 57 | int main( int argc, char * argv[] ) {
|
|---|
| 58 | int port = 8080;
|
|---|
| 59 | int backlog = 10;
|
|---|
| 60 | int nprocs = 1;
|
|---|
| 61 | int nworkers = 1;
|
|---|
| 62 | int cl_flags = 0;
|
|---|
| 63 | int chan_size = 10;
|
|---|
| 64 | const char * path = ".";
|
|---|
| 65 | //===================
|
|---|
| 66 | // Parse args
|
|---|
| [7f389a5c] | 67 | static cfa_option opt[] = {
|
|---|
| 68 | {'p', "port", "Port the server will listen on", port},
|
|---|
| 69 | {'c', "cpus", "Number of processors to use", nprocs},
|
|---|
| 70 | {'t', "threads", "Number of worker threads to use", nworkers},
|
|---|
| 71 | {'b', "accept-backlog", "Maximum number of pending accepts", backlog},
|
|---|
| 72 | {'B', "channel-size", "Maximum number of accepted connection pending", chan_size}
|
|---|
| 73 | };
|
|---|
| 74 | int opt_cnt = sizeof(opt) / sizeof(cfa_option);
|
|---|
| 75 |
|
|---|
| 76 | char **left;
|
|---|
| 77 | parse_args( argc, argv, opt, opt_cnt, "[OPTIONS] [PATH] -- cforall http server", left );
|
|---|
| 78 |
|
|---|
| [0aec496] | 79 |
|
|---|
| 80 | //===================
|
|---|
| 81 | // Open Files
|
|---|
| 82 | printf("Filling cache from %s\n", path);
|
|---|
| 83 | fill_cache( path );
|
|---|
| 84 |
|
|---|
| 85 | //===================
|
|---|
| 86 | // Open Socket
|
|---|
| 87 | printf("Listening on port %d\n", port);
|
|---|
| 88 | int server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|---|
| 89 | if(server_fd < 0) {
|
|---|
| 90 | abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) );
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| [7f389a5c] | 93 | int ret = 0;
|
|---|
| [0aec496] | 94 | struct sockaddr_in address;
|
|---|
| 95 | int addrlen = sizeof(address);
|
|---|
| 96 | memset( (char *)&address, '\0' );
|
|---|
| 97 | address.sin_family = AF_INET;
|
|---|
| 98 | address.sin_addr.s_addr = htonl(INADDR_ANY);
|
|---|
| 99 | address.sin_port = htons( port );
|
|---|
| 100 |
|
|---|
| 101 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) );
|
|---|
| 102 | if(ret < 0) {
|
|---|
| 103 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) );
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | ret = listen( server_fd, backlog );
|
|---|
| 107 | if(ret < 0) {
|
|---|
| 108 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | //===================
|
|---|
| 112 | // Run Server Cluster
|
|---|
| 113 | {
|
|---|
| 114 | cluster cl = { "Server Cluster", cl_flags };
|
|---|
| 115 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 116 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
|
|---|
| 117 | #endif
|
|---|
| 118 | options.the_cluster = &cl;
|
|---|
| 119 |
|
|---|
| 120 | channel chan = { chan_size };
|
|---|
| 121 | &wait_connect = &chan;
|
|---|
| 122 |
|
|---|
| 123 | {
|
|---|
| 124 | ServerProc procs[nprocs];
|
|---|
| 125 | {
|
|---|
| 126 | Worker workers[nworkers];
|
|---|
| 127 | printf("%d workers started on %d processors\n", nworkers, nprocs);
|
|---|
| 128 | {
|
|---|
| 129 | Acceptor acceptor = { server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen, 0 };
|
|---|
| 130 | }
|
|---|
| 131 | printf("Shutting Down\n");
|
|---|
| 132 |
|
|---|
| 133 | // Clean-up the workers
|
|---|
| 134 | for(nworkers) {
|
|---|
| 135 | put( wait_connect, -1 );
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | //===================
|
|---|
| 142 | // Close Socket
|
|---|
| 143 | printf("Closing Socket\n");
|
|---|
| 144 | ret = close( server_fd );
|
|---|
| 145 | if(ret < 0) {
|
|---|
| 146 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | //===================
|
|---|
| 150 | // Close Files
|
|---|
| 151 | printf("Closing Files\n");
|
|---|
| 152 | close_cache();
|
|---|
| 153 | }
|
|---|