Changeset 490
- Timestamp:
- 04/08/07 15:29:03 (2 years ago)
- Files:
-
- branches/multithreaded/server/ChangeLog (modified) (1 diff)
- branches/multithreaded/server/items.c (modified) (11 diffs)
- branches/multithreaded/server/memcached.c (modified) (76 diffs)
- branches/multithreaded/server/slabs.c (modified) (19 diffs)
- branches/multithreaded/server/t/stats.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/multithreaded/server/ChangeLog
r487 r490 1 2007-04-08 Paul Lindner <lindner@inuus.com> 2 3 * Add cleanup patch from "Tim Yardley" <liquid@haveheart.com> to 4 clean up source spacing issues, fix -Wall warnings, add some 5 null checks, adds asserts at the top of each function for any 6 use of conn *c without checking to see if c is NULL first. 7 1 8 2007-04-04 Paul Lindner <lindner@inuus.com> 2 9 branches/multithreaded/server/items.c
r487 r490 33 33 void item_init(void) { 34 34 int i; 35 for(i =0; i<LARGEST_ID; i++) {36 heads[i] =NULL;37 tails[i] =NULL;38 sizes[i] =0;35 for(i = 0; i < LARGEST_ID; i++) { 36 heads[i] = NULL; 37 tails[i] = NULL; 38 sizes[i] = 0; 39 39 } 40 40 } … … 74 74 item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 75 75 uint8_t nsuffix; 76 size_t ntotal;77 76 item *it; 78 unsigned int id;79 77 char suffix[40]; 80 81 ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 82 83 id = slabs_clsid(ntotal); 78 size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 79 80 unsigned int id = slabs_clsid(ntotal); 84 81 if (id == 0) 85 82 return 0; … … 104 101 105 102 if (id > LARGEST_ID) return NULL; 106 if (tails[id] ==0) return NULL;107 108 for (search = tails[id]; tries >0 && search; tries--, search=search->prev) {109 if (search->refcount ==0) {103 if (tails[id] == 0) return NULL; 104 105 for (search = tails[id]; tries > 0 && search; tries--, search=search->prev) { 106 if (search->refcount == 0) { 110 107 if (search->exptime > current_time) { 111 108 STATS_LOCK(); … … 118 115 } 119 116 it = slabs_alloc(ntotal); 120 if (it ==0) return NULL;117 if (it == 0) return NULL; 121 118 } 122 119 … … 135 132 strcpy(ITEM_key(it), key); 136 133 it->exptime = exptime; 137 memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix);134 memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix); 138 135 it->nsuffix = nsuffix; 139 136 return it; … … 271 268 /*@null@*/ 272 269 char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 273 int memlimit = 2 *1024*1024;270 int memlimit = 2097152; /* 2097152: (2 * 1024 * 1024) */ 274 271 char *buffer; 275 272 unsigned int bufcurr; … … 286 283 bufcurr = 0; 287 284 288 while (it != NULL && (limit ==0 || shown < limit)) {285 while (it != NULL && (limit == 0 || shown < limit)) { 289 286 len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 290 287 if (bufcurr + len + 6 > memlimit) /* 6 is END\r\n\0 */ 291 288 break; 292 289 strcpy(buffer + bufcurr, temp); 293 bufcurr +=len;290 bufcurr += len; 294 291 shown++; 295 292 it = it->next; 296 293 } 297 294 298 strcpy(buffer +bufcurr, "END\r\n");299 bufcurr +=5;295 strcpy(buffer + bufcurr, "END\r\n"); 296 bufcurr += 5; 300 297 301 298 *bytes = bufcurr; … … 313 310 } 314 311 315 for (i =0; i<LARGEST_ID; i++) {312 for (i = 0; i < LARGEST_ID; i++) { 316 313 if (tails[i]) 317 314 bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", … … 326 323 char* item_stats_sizes(int *bytes) { 327 324 const int num_buckets = 32768; /* max 1MB object, divided into 32 bytes size buckets */ 328 unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int));329 char *buf = (char *) malloc(1024*1024*2*sizeof(char));325 unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int)); 326 char *buf = (char *)malloc(2097152 * sizeof(char)); /* 2097152: 2 * 1024 * 1024 */ 330 327 int i; 331 328 … … 337 334 338 335 /* build the histogram */ 339 memset(histogram, 0, (size_t) num_buckets * sizeof(int));340 for (i =0; i<LARGEST_ID; i++) {336 memset(histogram, 0, (size_t)num_buckets * sizeof(int)); 337 for (i = 0; i < LARGEST_ID; i++) { 341 338 item *iter = heads[i]; 342 339 while (iter) { … … 351 348 /* write the buffer */ 352 349 *bytes = 0; 353 for (i =0; i<num_buckets; i++) {350 for (i = 0; i < num_buckets; i++) { 354 351 if (histogram[i] != 0) { 355 *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i *32, histogram[i]);352 *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i * 32, histogram[i]); 356 353 } 357 354 } branches/multithreaded/server/memcached.c
r487 r490 98 98 99 99 void pre_gdb(void); 100 void conn_free(conn *c); 100 101 101 102 /** exported globals **/ … … 116 117 117 118 static int *buckets = 0; /* bucket->generation array for a managed instance */ 118 119 void conn_free(conn *c);120 119 121 120 #define REALTIME_MAXDELTA 60*60*24*30 … … 138 137 really expiring never */ 139 138 if (exptime <= stats.started) 140 return (rel_time_t) 1;141 return (rel_time_t) (exptime - stats.started);139 return (rel_time_t)1; 140 return (rel_time_t)(exptime - stats.started); 142 141 } else { 143 return (rel_time_t) (exptime + current_time);142 return (rel_time_t)(exptime + current_time); 144 143 } 145 144 } … … 171 170 settings.udpport = 0; 172 171 settings.interface.s_addr = htonl(INADDR_ANY); 173 settings.maxbytes = 6 4*1024*1024; /* default is 64MB*/172 settings.maxbytes = 67108864; /* default is 64MB: (64 * 1024 * 1024) */ 174 173 settings.maxconns = 1024; /* to limit connections-related memory to about 5MB */ 175 174 settings.verbose = 0; … … 205 204 struct msghdr *msg; 206 205 206 assert(c != NULL); 207 207 208 if (c->msgsize == c->msgused) { 208 209 msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr)); … … 247 248 freetotal = 200; 248 249 freecurr = 0; 249 freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 250 /** TODO check for NULL **/ 250 if (!(freeconns = (conn **)malloc(sizeof(conn *) * freetotal))) { 251 perror("malloc()"); 252 } 251 253 return; 252 254 } … … 278 280 } else { 279 281 /* try to enlarge free connections array */ 280 conn **new_freeconns = realloc(freeconns, sizeof(conn *) *freetotal*2);282 conn **new_freeconns = realloc(freeconns, sizeof(conn *) * freetotal * 2); 281 283 if (new_freeconns) { 282 284 freetotal *= 2; … … 311 313 c->hdrsize = 0; 312 314 313 c->rbuf = (char *) malloc((size_t)c->rsize);314 c->wbuf = (char *) malloc((size_t)c->wsize);315 c->ilist = (item **) malloc(sizeof(item *) * c->isize);316 c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize);317 c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * c->msgsize);315 c->rbuf = (char *)malloc((size_t)c->rsize); 316 c->wbuf = (char *)malloc((size_t)c->wsize); 317 c->ilist = (item **)malloc(sizeof(item *) * c->isize); 318 c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); 319 c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize); 318 320 319 321 if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 || … … 383 385 384 386 static void conn_cleanup(conn *c) { 387 assert(c != NULL); 388 385 389 if (c->item) { 386 390 item_remove(c->item); … … 422 426 423 427 static void conn_close(conn *c) { 428 assert(c != NULL); 429 424 430 /* delete the event, the socket and the conn */ 425 431 event_del(&c->event); … … 454 460 */ 455 461 static void conn_shrink(conn *c) { 462 assert(c != NULL); 463 456 464 if (c->udp) 457 465 return; … … 459 467 if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 460 468 if (c->rcurr != c->rbuf) 461 memmove(c->rbuf, c->rcurr, (size_t) c->rbytes);462 463 char *newbuf = (char *) realloc((void*)c->rbuf, DATA_BUFFER_SIZE);469 memmove(c->rbuf, c->rcurr, (size_t)c->rbytes); 470 471 char *newbuf = (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE); 464 472 465 473 if (newbuf) { … … 472 480 473 481 if (c->isize > ITEM_LIST_HIGHWAT) { 474 item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0]));482 item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); 475 483 if (newbuf) { 476 484 c->ilist = newbuf; … … 481 489 482 490 if (c->msgsize > MSG_LIST_HIGHWAT) { 483 struct msghdr *newbuf = (struct msghdr *) realloc((void*)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0]));491 struct msghdr *newbuf = (struct msghdr *) realloc((void *)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); 484 492 if (newbuf) { 485 493 c->msglist = newbuf; … … 490 498 491 499 if (c->iovsize > IOV_LIST_HIGHWAT) { 492 struct iovec * newbuf = (struct iovec *) realloc((void*)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0]));500 struct iovec *newbuf = (struct iovec *) realloc((void *)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); 493 501 if (newbuf) { 494 502 c->iov = newbuf; … … 505 513 */ 506 514 static void conn_set_state(conn *c, int state) { 515 assert(c != NULL); 516 507 517 if (state != c->state) { 508 518 if (state == conn_read) { … … 522 532 */ 523 533 static int ensure_iov_space(conn *c) { 534 assert(c != NULL); 535 524 536 if (c->iovused >= c->iovsize) { 525 537 int i, iovnum; 526 struct iovec *new_iov = (struct iovec *) realloc(c->iov,538 struct iovec *new_iov = (struct iovec *)realloc(c->iov, 527 539 (c->iovsize * 2) * sizeof(struct iovec)); 528 540 if (! new_iov) … … 554 566 bool limit_to_mtu; 555 567 568 assert(c != NULL); 569 556 570 do { 557 571 m = &c->msglist[c->msgused - 1]; … … 582 596 583 597 m = &c->msglist[c->msgused - 1]; 584 m->msg_iov[m->msg_iovlen].iov_base = (void *)buf;598 m->msg_iov[m->msg_iovlen].iov_base = (void *)buf; 585 599 m->msg_iov[m->msg_iovlen].iov_len = len; 586 600 … … 603 617 int i; 604 618 unsigned char *hdr; 619 620 assert(c != NULL); 605 621 606 622 if (c->msgused > c->hdrsize) { … … 612 628 if (! new_hdrbuf) 613 629 return -1; 614 c->hdrbuf = (unsigned char *) new_hdrbuf;630 c->hdrbuf = (unsigned char *)new_hdrbuf; 615 631 c->hdrsize = c->msgused * 2; 616 632 } … … 628 644 *hdr++ = 0; 629 645 *hdr++ = 0; 630 assert((void *) hdr == (void*)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE);646 assert((void *) hdr == (void *)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); 631 647 } 632 648 … … 638 654 int len; 639 655 656 assert(c != NULL); 657 640 658 if (settings.verbose > 1) 641 659 fprintf(stderr, ">%d %s\n", c->sfd, str); 642 660 643 661 len = strlen(str); 644 if ( len + 2> c->wsize) {662 if ((len + 2) > c->wsize) { 645 663 /* ought to be always enough. just fail for simplicity */ 646 664 str = "SERVER_ERROR output line too long"; … … 664 682 665 683 static void complete_nread(conn *c) { 684 assert(c != NULL); 685 666 686 item *it = c->item; 667 687 int comm = c->item_comm; … … 726 746 727 747 typedef struct token_s { 728 char *value;748 char *value; 729 749 size_t length; 730 750 } token_t; … … 754 774 * } 755 775 */ 756 static size_t tokenize_command(char * command, token_t*tokens, const size_t max_tokens) {757 char *cp;758 char *value = NULL;776 static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) { 777 char *cp; 778 char *value = NULL; 759 779 size_t length = 0; 760 780 size_t ntokens = 0; … … 801 821 802 822 inline void process_stats_detail(conn *c, const char *command) { 823 assert(c != NULL); 824 803 825 if (strcmp(command, "on") == 0) { 804 826 settings.detail_enabled = 1; … … 828 850 } 829 851 830 static void process_stat(conn *c, token_t *tokens, const size_t ntokens) {852 static void process_stat(conn *c, token_t *tokens, const size_t ntokens) { 831 853 rel_time_t now = current_time; 832 char* command; 833 char* subcommand; 854 char *command; 855 char *subcommand; 856 857 assert(c != NULL); 834 858 835 859 if(ntokens < 2) { … … 853 877 pos += sprintf(pos, "STAT time %ld\r\n", now + stats.started); 854 878 pos += sprintf(pos, "STAT version " VERSION "\r\n"); 855 pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *));879 pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *)); 856 880 pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec); 857 881 pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); … … 869 893 pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read); 870 894 pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written); 871 pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long) settings.maxbytes);895 pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long)settings.maxbytes); 872 896 pos += sprintf(pos, "STAT threads %u\r\n", settings.num_threads); 873 897 pos += sprintf(pos, "END"); … … 915 939 int res; 916 940 917 wbuf = (char *)malloc(wsize); 918 if (wbuf == 0) { 941 if (!(wbuf = (char *)malloc(wsize))) { 919 942 out_string(c, "SERVER_ERROR out of memory"); 920 943 return; … … 940 963 } 941 964 strcpy(wbuf + res, "END\r\n"); 942 c->write_and_free =wbuf;943 c->wcurr =wbuf;965 c->write_and_free = wbuf; 966 c->wcurr = wbuf; 944 967 c->wbytes = res + 5; // Don't write the terminal '\0' 945 968 conn_set_state(c, conn_write); … … 981 1004 } 982 1005 983 if (strcmp(subcommand, "slabs") ==0) {1006 if (strcmp(subcommand, "slabs") == 0) { 984 1007 int bytes = 0; 985 1008 char *buf = slabs_stats(&bytes); … … 996 1019 } 997 1020 998 if (strcmp(subcommand, "items") ==0) {1021 if (strcmp(subcommand, "items") == 0) { 999 1022 char buffer[4096]; 1000 1023 item_stats(buffer, 4096); … … 1003 1026 } 1004 1027 1005 if (strcmp(subcommand, "detail") ==0) {1028 if (strcmp(subcommand, "detail") == 0) { 1006 1029 if (ntokens < 4) 1007 1030 process_stats_detail(c, ""); /* outputs the error message */ … … 1011 1034 } 1012 1035 1013 if (strcmp(subcommand, "sizes") ==0) {1036 if (strcmp(subcommand, "sizes") == 0) { 1014 1037 int bytes = 0; 1015 1038 char *buf = item_stats_sizes(&bytes); … … 1031 1054 1032 1055 /* ntokens is overwritten here... shrug.. */ 1033 static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) {1056 static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) { 1034 1057 char *key; 1035 1058 size_t nkey; 1036 1059 int i = 0; 1037 1060 item *it; 1038 token_t* key_token = &tokens[KEY_TOKEN]; 1061 token_t *key_token = &tokens[KEY_TOKEN]; 1062 1063 assert(c != NULL); 1039 1064 1040 1065 if (settings.managed) { … … 1071 1096 if (it) { 1072 1097 if (i >= c->isize) { 1073 item **new_list = realloc(c->ilist, sizeof(item *) *c->isize*2);1098 item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2); 1074 1099 if (new_list) { 1075 1100 c->isize *= 2; … … 1139 1164 } 1140 1165 1141 static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm) {1166 static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm) { 1142 1167 char *key; 1143 1168 size_t nkey; … … 1146 1171 int vlen; 1147 1172 item *it; 1173 1174 assert(c != NULL); 1148 1175 1149 1176 if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { … … 1190 1217 /* swallow the data line */ 1191 1218 c->write_and_go = conn_swallow; 1192 c->sbytes = vlen +2;1219 c->sbytes = vlen + 2; 1193 1220 return; 1194 1221 } … … 1201 1228 } 1202 1229 1203 static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) {1230 static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) { 1204 1231 char temp[32]; 1205 1232 item *it; … … 1207 1234 char *key; 1208 1235 size_t nkey; 1209 char *msg;1210 1236 1237 assert(c != NULL); 1238 1211 1239 if(tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { 1212 1240 out_string(c, "CLIENT_ERROR bad command line format"); … … 1263 1291 1264 1292 ptr = ITEM_data(it); 1265 while ((*ptr != '\0') && (*ptr <'0' && *ptr>'9')) ptr++; // BUG: can't be true1293 while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++; // BUG: can't be true 1266 1294 1267 1295 value = strtol(ptr, NULL, 10); … … 1272 1300 1273 1301 if (incr != 0) 1274 value +=delta;1302 value += delta; 1275 1303 else { 1276 1304 if (delta >= value) value = 0; 1277 else value -=delta;1305 else value -= delta; 1278 1306 } 1279 1307 snprintf(buf, 32, "%u", value); … … 1291 1319 } else { /* replace in-place */ 1292 1320 memcpy(ITEM_data(it), buf, res); 1293 memset(ITEM_data(it) + res, ' ', it->nbytes -res-2);1321 memset(ITEM_data(it) + res, ' ', it->nbytes - res - 2); 1294 1322 } 1295 1323 … … 1297 1325 } 1298 1326 1299 static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) {1327 static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) { 1300 1328 char *key; 1301 1329 size_t nkey; … … 1303 1331 time_t exptime = 0; 1304 1332 1333 assert(c != NULL); 1334 1305 1335 if (settings.managed) { 1306 1336 int bucket = c->bucket; … … 1388 1418 int comm; 1389 1419 1420 assert(c != NULL); 1421 1390 1422 if (settings.verbose > 1) 1391 1423 fprintf(stderr, "<%d %s\n", c->sfd, command); … … 1477 1509 return; 1478 1510 } 1479 if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) {1511 if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) { 1480 1512 /* we never write anything back, even if input's wrong */ 1481 if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <=0)) {1513 if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <= 0)) { 1482 1514 /* do nothing, bad input */ 1483 1515 } else { … … 1569 1601 char *el, *cont; 1570 1602 1571 assert(c->rcurr <= c->rbuf + c->rsize); 1603 assert(c != NULL); 1604 assert(c->rcurr <= (c->rbuf + c->rsize)); 1572 1605 1573 1606 if (c->rbytes == 0) … … 1577 1610 return 0; 1578 1611 cont = el + 1; 1579 if ( el - c->rcurr> 1 && *(el - 1) == '\r') {1612 if ((el - c->rcurr) > 1 && *(el - 1) == '\r') { 1580 1613 el--; 1581 1614 } 1582 1615 *el = '\0'; 1583 1616 1584 assert(cont <= c->rcurr + c->rbytes);1617 assert(cont <= (c->rcurr + c->rbytes)); 1585 1618 1586 1619 process_command(c, c->rcurr); … … 1589 1622 c->rcurr = cont; 1590 1623 1591 assert(c->rcurr <= c->rbuf + c->rsize);1624 assert(c->rcurr <= (c->rbuf + c->rsize)); 1592 1625 1593 1626 return 1; … … 1600 1633 static int try_read_udp(conn *c) { 1601 1634 int res; 1635 1636 assert(c != NULL); 1602 1637 1603 1638 c->request_addr_size = sizeof(c->request_addr); … … 1641 1676 int res; 1642 1677 1678 assert(c != NULL); 1679 1643 1680 if (c->rcurr != c->rbuf) { 1644 1681 if (c->rbytes != 0) /* otherwise there's nothing to copy */ … … 1649 1686 while (1) { 1650 1687 if (c->rbytes >= c->rsize) { 1651 char *new_rbuf = realloc(c->rbuf, c->rsize *2);1688 char *new_rbuf = realloc(c->rbuf, c->rsize * 2); 1652 1689 if (!new_rbuf) { 1653 1690 if (settings.verbose > 0) … … 1658 1695 return 1; 1659 1696 } 1660 c->rcurr = c->rbuf = new_rbuf;1697 c->rcurr = c->rbuf = new_rbuf; 1661 1698 c->rsize *= 2; 1662 1699 } … … 1694 1731 1695 1732 static bool update_event(conn *c, const int new_flags) { 1733 assert(c != NULL); 1734 1696 1735 struct event_base *base = c->event.ev_base; 1697 1736 if (c->ev_flags == new_flags) … … 1738 1777 int res; 1739 1778 1779 assert(c != NULL); 1780 1740 1781 if (c->msgcurr < c->msgused && 1741 1782 c->msglist[c->msgcurr].msg_iovlen == 0) { … … 1796 1837 socklen_t addrlen; 1797 1838 struct sockaddr addr; 1798 conn *newc;1799 1839 int res; 1840 1841 assert(c != NULL); 1800 1842 1801 1843 while (!stop) { … … 2007 2049 2008 2050 c = (conn *)arg; 2051 assert(c != NULL); 2052 2009 2053 c->which = which; 2010 2054 … … 2063 2107 2064 2108 while (min <= max) { 2065 avg = ((unsigned int) min + max) / 2;2109 avg = ((unsigned int)(min + max)) / 2; 2066 2110 if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &avg, intsize) == 0) { 2067 2111 last_good = avg; … … 2105 2149 addr.sin_port = htons(port); 2106 2150 addr.sin_addr = settings.interface; 2107 if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {2151 if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 2108 2152 perror("bind()"); 2109 2153 close(sfd); … … 2171 2215 addr.sun_family = AF_UNIX; 2172 2216 strcpy(addr.sun_path, path); 2173 if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {2217 if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 2174 2218 perror("bind()"); 2175 2219 close(sfd); … … 2185 2229 2186 2230 /* listening socket */ 2187 static int l_socket =0;2231 static int l_socket = 0; 2188 2232 2189 2233 /* udp socket */ 2190 static int u_socket =-1;2234 static int u_socket = -1; 2191 2235 2192 2236 /* invoke right before gdb is called, on assert */ 2193 2237 void pre_gdb(void) { 2194 int i = 0;2238 int i; 2195 2239 if (l_socket > -1) close(l_socket); 2196 2240 if (u_socket > -1) close(u_socket); 2197 for (i =3; i<=500; i++) close(i); /* so lame */2241 for (i = 3; i <= 500; i++) close(i); /* so lame */ 2198 2242 kill(getpid(), SIGABRT); 2199 2243 } … … 2256 2300 void do_run_deferred_deletes(void) 2257 2301 { 2258 int i =0, j=0;2259 2260 for (i =0; i<delcurr; i++) {2302 int i, j = 0; 2303 2304 for (i = 0; i < delcurr; i++) { 2261 2305 item *it = todelete[i]; 2262 2306 if (item_delete_lock_over(it)) { … … 2375 2419 return; 2376 2420 2377 if (!(fp = fopen(pid_file, "w"))) {2378 fprintf(stderr, "Could not open the pid file %s for writing\n",pid_file);2421 if (!(fp = fopen(pid_file, "w"))) { 2422 fprintf(stderr, "Could not open the pid file %s for writing\n", pid_file); 2379 2423 return; 2380 2424 } 2381 2425 2382 fprintf(fp,"%ld\n", (long)pid);2426 fprintf(fp,"%ld\n", (long)pid); 2383 2427 if (fclose(fp) == -1) { 2384 fprintf(stderr, "Could not close the pid file %s.\n",pid_file);2428 fprintf(stderr, "Could not close the pid file %s.\n", pid_file); 2385 2429 return; 2386 2430 } … … 2392 2436 2393 2437 if (unlink(pid_file) != 0) { 2394 fprintf(stderr, "Could not remove the pid file %s.\n",pid_file);2438 fprintf(stderr, "Could not remove the pid file %s.\n", pid_file); 2395 2439 } 2396 2440 … … 2405 2449 int main (int argc, char **argv) { 2406 2450 int c; 2407 conn *u_conn;2408 2451 struct in_addr addr; 2409 2452 bool lock_memory = false; … … 2441 2484 break; 2442 2485 case 'm': 2443 settings.maxbytes = ((size_t)atoi(optarg)) *1024*1024;2486 settings.maxbytes = ((size_t)atoi(optarg)) * 1024 * 1024; 2444 2487 break; 2445 2488 case 'M': … … 2522 2565 * the soft limit to the hard. 2523 2566 */ 2524 if (getrlimit(RLIMIT_CORE, &rlim) ==0) {2567 if (getrlimit(RLIMIT_CORE, &rlim) == 0) { 2525 2568 rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY; 2526 if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) {2569 if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) { 2527 2570 /* failed. try raising just to the old max */ 2528 rlim_new.rlim_cur = rlim_new.rlim_max = 2529 rlim.rlim_max; 2530 (void) setrlimit(RLIMIT_CORE, &rlim_new); 2571 rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max; 2572 (void)setrlimit(RLIMIT_CORE, &rlim_new); 2531 2573 } 2532 2574 } … … 2537 2579 */ 2538 2580 2539 if ((getrlimit(RLIMIT_CORE, &rlim) !=0) || rlim.rlim_cur==0) {2581 if ((getrlimit(RLIMIT_CORE, &rlim) != 0) || rlim.rlim_cur == 0) { 2540 2582 fprintf(stderr, "failed to ensure corefile creation\n"); 2541 2583 exit(EXIT_FAILURE); … … 2589 2631 2590 2632 /* lose root privileges if we have them */ 2591 if (getuid() == 0 || geteuid()==0) {2592 if (username ==0 || *username=='\0') {2633 if (getuid() == 0 || geteuid() == 0) { 2634 if (username == 0 || *username == '\0') { 2593 2635 fprintf(stderr, "can't run as root without the -u switch\n"); 2594 2636 return 1; … … 2598 2640 return 1; 2599 2641 } 2600 if (setgid(pw->pw_gid) <0 || setuid(pw->pw_uid)<0) {2642 if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { 2601 2643 fprintf(stderr, "failed to assume identity of user %s\n", username); 2602 2644 return 1; … … 2636 2678 /* managed instance? alloc and zero a bucket array */ 2637 2679 if (settings.managed) { 2638 buckets = malloc(sizeof(int) *MAX_BUCKETS);2680 buckets = malloc(sizeof(int) * MAX_BUCKETS); 2639 2681 if (buckets == 0) { 2640 2682 fprintf(stderr, "failed to allocate the bucket array"); 2641 2683 exit(EXIT_FAILURE); 2642 2684 } 2643 memset(buckets, 0, sizeof(int) *MAX_BUCKETS);2685 memset(buckets, 0, sizeof(int) * MAX_BUCKETS); 2644 2686 } 2645 2687 … … 2672 2714 /* save the PID in if we're a daemon */ 2673 2715 if (daemonize) 2674 save_pid(getpid(), pid_file);2716 save_pid(getpid(), pid_file); 2675 2717 /* start up worker threads if MT mode */ 2676 2718 thread_init(settings.num_threads, main_base); 2677 2719 /* initialise clock event */ 2678 clock_handler(0, 0,0);2720 clock_handler(0, 0, 0); 2679 2721 /* initialise deletion array and timer event */ 2680 deltotal = 200; delcurr = 0; 2681 todelete = malloc(sizeof(item *)*deltotal); 2682 delete_handler(0,0,0); /* sets up the event */ 2722 deltotal = 200; 2723 delcurr = 0; 2724 todelete = malloc(sizeof(item *) * deltotal); 2725 delete_handler(0, 0, 0); /* sets up the event */ 2683 2726 /* create the initial listening udp connection, monitored on all threads */ 2684 2727 if (u_socket > -1) { branches/multithreaded/server/slabs.c
r469 r490 50 50 } slabclass_t; 51 51 52 static slabclass_t slabclass[POWER_LARGEST +1];52 static slabclass_t slabclass[POWER_LARGEST + 1]; 53 53 static size_t mem_limit = 0; 54 54 static size_t mem_malloced = 0; … … 60 60 static int do_slabs_newslab(const unsigned int id); 61 61 62 #ifndef DONT_PREALLOC_SLABS 62 63 /* Preallocate as many slab pages as possible (called from slabs_init) 63 64 on start-up, so users don't get confused out-of-memory errors when … … 67 68 smaller ones will be made. */ 68 69 static void slabs_preallocate (const unsigned int maxslabs); 69 70 #endif 70 71 71 72 /* … … 80 81 int res = POWER_SMALLEST; 81 82 82 if(size ==0)83 if(size == 0) 83 84 return 0; 84 85 while (size > slabclass[res].size) … … 125 126 char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC"); 126 127 if (t_initial_malloc) { 127 mem_malloced = (size_t) atol(t_initial_malloc);128 mem_malloced = (size_t)atol(t_initial_malloc); 128 129 } 129 130 … … 141 142 } 142 143 144 #ifndef DONT_PREALLOC_SLABS 143 145 static void slabs_preallocate (const unsigned int maxslabs) { 144 146 int i; … … 151 153 these three lines. */ 152 154 153 for(i =POWER_SMALLEST; i<=POWER_LARGEST; i++) {155 for(i = POWER_SMALLEST; i <= POWER_LARGEST; i++) { 154 156 if (++prealloc > maxslabs) 155 157 return; … … 158 160 159 161 } 162 #endif 160 163 161 164 static int grow_slab_list (const unsigned int id) { … … 163 166 if (p->slabs == p->list_size) { 164 167 size_t new_size = (p->list_size != 0) ? p->list_size * 2 : 16; 165 void *new_list = realloc(p->slab_list, new_size *sizeof(void*));168 void *new_list = realloc(p->slab_list, new_size * sizeof(void *)); 166 169 if (new_list == 0) return 0; 167 170 p->list_size = new_size; … … 206 209 207 210 p = &slabclass[id]; 208 assert(p->sl_curr == 0 || ((item *)p->slots[p->sl_curr-1])->slabs_clsid == 0);211 assert(p->sl_curr == 0 || ((item *)p->slots[p->sl_curr - 1])->slabs_clsid == 0); 209 212 210 213 #ifdef USE_SYSTEM_MALLOC … … 217 220 /* fail unless we have space at the end of a recently allocated page, 218 221 we have something on our freelist, or we could allocate a new page */ 219 if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || do_slabs_newslab(id) != 0))222 if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || do_slabs_newslab(id) != 0)) 220 223 return 0; 221 224 … … 242 245 slabclass_t *p; 243 246 244 assert(((item *)ptr)->slabs_clsid ==0);247 assert(((item *)ptr)->slabs_clsid == 0); 245 248 assert(id >= POWER_SMALLEST && id <= power_largest); 246 249 if (id < POWER_SMALLEST || id > power_largest) … … 256 259 257 260 if (p->sl_curr == p->sl_total) { /* need more space on the free list */ 258 int new_size = (p->sl_total != 0) ? p->sl_total *2 : 16; /* 16 is arbitrary */259 void **new_slots = realloc(p->slots, new_size *sizeof(void *));261 int new_size = (p->sl_total != 0) ? p->sl_total * 2 : 16; /* 16 is arbitrary */ 262 void **new_slots = realloc(p->slots, new_size * sizeof(void *)); 260 263 if (new_slots == 0) 261 264 return; … … 270 273 char* do_slabs_stats(int *buflen) { 271 274 int i, total; 272 char *buf = (char *)malloc(power_largest * 200 + 100);275 char *buf = (char *)malloc(power_largest * 200 + 100); 273 276 char *bufcurr = buf; 274 277 … … 295 298 } 296 299 } 297 bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long) mem_malloced);300 bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long)mem_malloced); 298 301 bufcurr += sprintf(bufcurr, "END\r\n"); 299 302 *buflen = bufcurr - buf; … … 333 336 if (p->killing == 0) p->killing = 1; 334 337 335 slab = p->slab_list[p->killing -1];338 slab = p->slab_list[p->killing - 1]; 336 339 slab_end = slab + POWER_BLOCK; 337 340 338 for (iter =slab; iter<slab_end; iter+=p->size) {339 item *it = (item *) iter;341 for (iter = slab; iter < slab_end; iter += p->size) { 342 item *it = (item *)iter; 340 343 if (it->slabs_clsid) { 341 344 if (it->refcount) was_busy = 1; … … 347 350 { 348 351 int fi; 349 for (fi =p->sl_curr-1; fi>=0; fi--) {352 for (fi = p->sl_curr - 1; fi >= 0; fi--) { 350 353 if (p->slots[fi] >= slab && p->slots[fi] < slab_end) { 351 354 p->sl_curr--; … … 358 361 359 362 /* if good, now move it to the dst slab class */ 360 p->slab_list[p->killing -1] = p->slab_list[p->slabs-1];363 p->slab_list[p->killing - 1] = p->slab_list[p->slabs - 1]; 361 364 p->slabs--; 362 365 p->killing = 0; … … 366 369 /* this isn't too critical, but other parts of the code do asserts to 367 370 make sure this field is always 0. */ 368 for (iter =slab; iter<slab_end; iter+=dp->size) {371 for (iter = slab; iter < slab_end; iter += dp->size) { 369 372 ((item *)iter)->slabs_clsid = 0; 370 373 } branches/multithreaded/server/t/stats.t
r487 r490 2 2 3 3 use strict; 4 use Test::More tests => 1 6;4 use Test::More tests => 17; 5 5 use FindBin qw($Bin); 6 6 use lib "$Bin/lib";
