#define __USE_GNU #include #include #include #include extern "C" { #include #include } #include #include #include #include "channel.hfa" #include "filecache.hfa" #include "options.hfa" #include "parseargs.hfa" #include "worker.hfa" //============================================================================================= // Globals //============================================================================================= Options options @= { 0, 42u, 0, false, false, 0 }; channel & wait_connect; struct ServerProc { processor self; }; void ?{}( ServerProc & this ) { /* paranoid */ assert( options.the_cluster != 0p ); (this.self){ "Benchmark Processor", *options.the_cluster }; #if !defined(__CFA_NO_STATISTICS__) if( options.procstats ) { print_stats_at_exit( this.self, options.the_cluster->print_stats ); } if( options.viewhalts ) { print_halts( this.self ); } #endif } //============================================================================================= // Main //=============================================================================================' int main( int argc, char * argv[] ) { int port = 8080; int backlog = 10; int nprocs = 1; int nworkers = 1; int cl_flags = 0; int chan_size = 10; const char * path = "."; //=================== // Parse args static cfa_option opt[] = { {'p', "port", "Port the server will listen on", port}, {'c', "cpus", "Number of processors to use", nprocs}, {'t', "threads", "Number of worker threads to use", nworkers}, {'b', "accept-backlog", "Maximum number of pending accepts", backlog}, {'B', "channel-size", "Maximum number of accepted connection pending", chan_size} }; int opt_cnt = sizeof(opt) / sizeof(cfa_option); char **left; parse_args( argc, argv, opt, opt_cnt, "[OPTIONS] [PATH] -- cforall http server", left ); //=================== // Open Files printf("Filling cache from %s\n", path); fill_cache( path ); //=================== // Open Socket printf("Listening on port %d\n", port); int server_fd = socket(AF_INET, SOCK_STREAM, 0); if(server_fd < 0) { abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) ); } int ret = 0; struct sockaddr_in address; int addrlen = sizeof(address); memset( (char *)&address, '\0' ); address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_ANY); address.sin_port = htons( port ); ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); if(ret < 0) { abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); } ret = listen( server_fd, backlog ); if(ret < 0) { abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); } //=================== // Run Server Cluster { cluster cl = { "Server Cluster", cl_flags }; #if !defined(__CFA_NO_STATISTICS__) print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); #endif options.the_cluster = &cl; channel chan = { chan_size }; &wait_connect = &chan; { ServerProc procs[nprocs]; { Worker workers[nworkers]; printf("%d workers started on %d processors\n", nworkers, nprocs); { Acceptor acceptor = { server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen, 0 }; } printf("Shutting Down\n"); // Clean-up the workers for(nworkers) { put( wait_connect, -1 ); } } } } //=================== // Close Socket printf("Closing Socket\n"); ret = close( server_fd ); if(ret < 0) { abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); } //=================== // Close Files printf("Closing Files\n"); close_cache(); }