/* * File: hellolib.c * * Compile: * gcc -Wall -fPIC -c hellolib.c && gcc -shared -Wl -o libhellolib.so hellolib.o * * Calling from lua: * * > hello_lib = package.loadlib("/home/geckos/programming/lua/libhellolib.so", "lua_open_hellolib")() * > * > * > hello_lib.hello_from_c() * Hello from C world to lua * > */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <lua.h> #include <lualib.h> #include <lauxlib.h> static int hello_from_c(lua_State *L) { puts("Hello from C world to lua"); return 0; } static const struct luaL_reg hello_lib[] = { { "hello_from_c", hello_from_c }, { NULL, NULL }, }; int lua_open_hellolib(lua_State *L) { luaL_openlib(L, "hello_lib", hello_lib, 0); return 1; }
quarta-feira, 25 de janeiro de 2012
Creating C libraries/extensions/binds to Lua
I'm training the C API of lua.. planning to create extensions to awesome window manager (the one I use) -> http://awesome.naquadah.org/. Here is a "Lua calling C" hello world.
quinta-feira, 5 de janeiro de 2012
TCP Flooder with pthreads
/** * This code creates N threads. Each threads does one GET to an address. The * GET, the address and the number of threads are passed as argument. An "\r\n" * is appended to the GET string. */ /* headers *//*{{{*/ #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <pthread.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netdb.h>/*}}}*/ /* declarations and prototypes */ /*{{{*/ #define pexit(s) ({perror(s); exit(exit_failure);}) static struct cstats { int socket_errors; int send_errors; int recv_errors; int connect_errors; int avg_respt; /* average response time */ pthread_mutex_t mutex; } cstats; struct sockaddr_in addr; #define buflen 1024 static char buf[buflen]; static int buf_len; void *get_get_thread(void *);/*}}}*/ int main(int argc, char **argv) { /* main declarations *//*{{{*/ int i; int fail = 0; int ngets; int addr_len; int error; struct hostent *host; pthread_t *threadv; pthread_attr_t attr;/*}}}*/ /* error checking *//*{{{*/ if (argc <= 4) { printf("usage: %s address port number_of_gets get_string\n", argv[0]); exit(exit_failure); } ngets = atoi(argv[3]); if (ngets <= 0) { printf("number_of_gets\n"); errno = einval; exit(exit_failure); } /*}}}*/ bzero(&cstats, sizeof(struct cstats)); /* pthread initialization *//*{{{*/ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, pthread_create_joinable); pthread_mutex_init(&cstats.mutex, null); threadv = malloc(sizeof(pthread_t) * ngets); if (!threadv) pexit("malloc");/*}}}*/ /* address initialization *//*{{{*/ strncpy(buf, argv[4], buflen); strncat(buf, "\r\n", buflen); host = gethostbyname(argv[1]); if (!host) pexit("gethostbyname"); memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], sizeof(struct sockaddr_in)); addr.sin_family = pf_inet; addr.sin_port = htons(atoi(argv[2])); /*}}}*/ /* creating threads *//*{{{*/ for (i = 0; i < ngets; i++) { error = pthread_create(&threadv[i], &attr, get_get_thread, null); if (error) fail++; }/*}}}*/ printf("%d thread created. Running...\n", ngets - fail); /* joing threads *//*{{{*/ for (i = 0; i < ngets; i++) { error = pthread_join(threadv[i], null); if (error) perror("pthread_join"); }/*}}}*/ /* output *//*{{{*/ printf("errors:\n" " socket() errors: %d\n" " connect() errors: %d\n" " send() errors: %d\n" " recv() errors: %d\n", cstats.socket_errors, cstats.connect_errors, cstats.send_errors, cstats.recv_errors);/*}}}*/ return 0; } void *get_get_thread(void *dummy)/*{{{*/ { int error; int nbytes; int sock; #define RBUF_LEN 1024 char rbuf[RBUF_LEN]; sock = socket(PF_INET, SOCK_STREAM, 0);/*{{{*/ if (sock < 0) { pthread_mutex_lock(&cstats.mutex); cstats.socket_errors++; pthread_mutex_unlock(&cstats.mutex); }/*}}}*/ error = connect(sock, (struct sockaddr *)&addr, sizeof addr);/*{{{*/ if (error) { pthread_mutex_lock(&cstats.mutex); cstats.connect_errors++; pthread_mutex_unlock(&cstats.mutex); }/*}}}*/ nbytes = send(sock, buf, strlen(buf) , 0);/*{{{*/ if (nbytes == -1) /* error */ { pthread_mutex_lock(&cstats.mutex); cstats.send_errors++; pthread_mutex_unlock(&cstats.mutex); }/*}}}*/ nbytes = recv(sock, rbuf, RBUF_LEN, 0);/*{{{*/ if (nbytes == -1) { pthread_mutex_lock(&cstats.mutex); cstats.recv_errors++; pthread_mutex_unlock(&cstats.mutex); }/*}}}*/ close(sock); return (void *)0; }/*}}}*/ /* vim: set fdm=marker: tw=80 : */
Collectd patch to collect cpu average on linux
I'm using collectd to collectd data at work -> http://www.collectd.org
The cpu plugin don't collectd average cpu usage, I have create a patch to work this around. Here it is
diff -Nurp collectd-5.0.1/src/cpu.c collectd-5.0.1-new/src/cpu.c --- collectd-5.0.1/src/cpu.c 2011-10-14 17:49:49.000000000 -0300 +++ collectd-5.0.1-new/src/cpu.c 2012-01-03 17:48:22.000000000 -0200 @@ -252,7 +252,11 @@ static void submit (int cpu_num, const c vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin)); - ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), + if (cpu_num < 0) + ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), + "avg"); + else + ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", cpu_num); sstrncpy (vl.type, "cpu", sizeof (vl.type)); sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); @@ -359,6 +363,7 @@ static int cpu_read (void) char *fields[9]; int numfields; + int coren = sysconf(_SC_NPROCESSORS_ONLN); /* number of cores */ if ((fh = fopen ("/proc/stat", "r")) == NULL) { @@ -372,18 +377,24 @@ static int cpu_read (void) { if (strncmp (buf, "cpu", 3)) continue; - if ((buf[3] < '0') || (buf[3] > '9')) - continue; numfields = strsplit (buf, fields, 9); if (numfields < 5) continue; - cpu = atoi (fields[0] + 3); - user = atoll (fields[1]); - nice = atoll (fields[2]); - syst = atoll (fields[3]); - idle = atoll (fields[4]); + if (!isdigit(fields[0][3])) { + cpu = -1; + user = atoll (fields[1]) / coren; + nice = atoll (fields[2]) / coren; + syst = atoll (fields[3]) / coren; + idle = atoll (fields[4]) / coren; + } else { + cpu = atoi (fields[0] + 3); + user = atoll (fields[1]); + nice = atoll (fields[2]); + syst = atoll (fields[3]); + idle = atoll (fields[4]); + } submit (cpu, "user", user); submit (cpu, "nice", nice); @@ -392,9 +403,15 @@ static int cpu_read (void) if (numfields >= 8) { - wait = atoll (fields[5]); - intr = atoll (fields[6]); - sitr = atoll (fields[7]); + if (cpu < 0) { + wait = atoll (fields[5]) / coren; + intr = atoll (fields[6]) / coren; + sitr = atoll (fields[7]) / coren; + } else { + wait = atoll (fields[5]); + intr = atoll (fields[6]); + sitr = atoll (fields[7]); + } submit (cpu, "wait", wait); submit (cpu, "interrupt", intr);
Marcadores:
collectd,
collecting cpu average with collectd,
cpu plugin
Assinar:
Postagens (Atom)