Changeset 489
- Timestamp:
- 04/08/07 15:27:38 (2 years ago)
- Files:
-
- trunk/server/ChangeLog (modified) (1 diff)
- trunk/server/items.c (modified) (11 diffs)
- trunk/server/memcached.c (modified) (72 diffs)
- trunk/server/slabs.c (modified) (19 diffs)
- trunk/server/t/stats.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/server/ChangeLog
r484 r489 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 trunk/server/items.c
r476 r489 38 38 void item_init(void) { 39 39 int i; 40 for(i =0; i<LARGEST_ID; i++) {41 heads[i] =NULL;42 tails[i] =NULL;43 sizes[i] =0;40 for(i = 0; i < LARGEST_ID; i++) { 41 heads[i] = NULL; 42 tails[i] = NULL; 43 sizes[i] = 0; 44 44 } 45 45 } … … 68 68 item *item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 69 69 uint8_t nsuffix; 70 size_t ntotal;71 70 item *it; 72 unsigned int id;73 71 char suffix[40]; 74 75 ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 76 77 id = slabs_clsid(ntotal); 72 size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 73 74 unsigned int id = slabs_clsid(ntotal); 78 75 if (id == 0) 79 76 return 0; … … 98 95 99 96 if (id > LARGEST_ID) return NULL; 100 if (tails[id] ==0) return NULL;101 102 for (search = tails[id]; tries >0 && search; tries--, search=search->prev) {103 if (search->refcount ==0) {97 if (tails[id] == 0) return NULL; 98 99 for (search = tails[id]; tries > 0 && search; tries--, search=search->prev) { 100 if (search->refcount == 0) { 104 101 if (search->exptime > current_time) 105 102 stats.evictions++; … … 109 106 } 110 107 it = slabs_alloc(ntotal); 111 if (it ==0) return NULL;108 if (it == 0) return NULL; 112 109 } 113 110 … … 125 122 strcpy(ITEM_key(it), key); 126 123 it->exptime = exptime; 127 memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix);124 memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix); 128 125 it->nsuffix = nsuffix; 129 126 return it; … … 251 248 /*@null@*/ 252 249 char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 253 int memlimit = 2 *1024*1024;250 int memlimit = 2097152; /* 2097152: (2 * 1024 * 1024) */ 254 251 char *buffer; 255 252 unsigned int bufcurr; … … 266 263 bufcurr = 0; 267 264 268 while (it != NULL && (limit ==0 || shown < limit)) {265 while (it != NULL && (limit == 0 || shown < limit)) { 269 266 len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 270 267 if (bufcurr + len + 6 > memlimit) /* 6 is END\r\n\0 */ 271 268 break; 272 269 strcpy(buffer + bufcurr, temp); 273 bufcurr +=len;270 bufcurr += len; 274 271 shown++; 275 272 it = it->next; 276 273 } 277 274 278 strcpy(buffer +bufcurr, "END\r\n");279 bufcurr +=5;275 strcpy(buffer + bufcurr, "END\r\n"); 276 bufcurr += 5; 280 277 281 278 *bytes = bufcurr; … … 293 290 } 294 291 295 for (i =0; i<LARGEST_ID; i++) {292 for (i = 0; i < LARGEST_ID; i++) { 296 293 if (tails[i]) 297 294 bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", … … 306 303 char* item_stats_sizes(int *bytes) { 307 304 const int num_buckets = 32768; /* max 1MB object, divided into 32 bytes size buckets */ 308 unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int));309 char *buf = (char *) malloc(1024*1024*2*sizeof(char));305 unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int)); 306 char *buf = (char *)malloc(2097152 * sizeof(char)); /* 2097152: 2 * 1024 * 1024 */ 310 307 int i; 311 308 … … 317 314 318 315 /* build the histogram */ 319 memset(histogram, 0, (size_t) num_buckets * sizeof(int));320 for (i =0; i<LARGEST_ID; i++) {316 memset(histogram, 0, (size_t)num_buckets * sizeof(int)); 317 for (i = 0; i < LARGEST_ID; i++) { 321 318 item *iter = heads[i]; 322 319 while (iter) { … … 331 328 /* write the buffer */ 332 329 *bytes = 0; 333 for (i =0; i<num_buckets; i++) {330 for (i = 0; i < num_buckets; i++) { 334 331 if (histogram[i] != 0) { 335 *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i *32, histogram[i]);332 *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i * 32, histogram[i]); 336 333 } 337 334 } trunk/server/memcached.c
r482 r489 105 105 106 106 void pre_gdb(void); 107 static void conn_free(conn *c); 107 108 108 109 /** exported globals **/ … … 142 143 really expiring never */ 143 144 if (exptime <= stats.started) 144 return (rel_time_t) 1;145 return (rel_time_t) (exptime - stats.started);145 return (rel_time_t)1; 146 return (rel_time_t)(exptime - stats.started); 146 147 } else { 147 return (rel_time_t) (exptime + current_time);148 return (rel_time_t)(exptime + current_time); 148 149 } 149 150 } … … 171 172 settings.udpport = 0; 172 173 settings.interface.s_addr = htonl(INADDR_ANY); 173 settings.maxbytes = 6 4*1024*1024; /* default is 64MB*/174 settings.maxbytes = 67108864; /* default is 64MB: (64 * 1024 * 1024) */ 174 175 settings.maxconns = 1024; /* to limit connections-related memory to about 5MB */ 175 176 settings.verbose = 0; … … 229 230 struct msghdr *msg; 230 231 232 assert(c != NULL); 233 231 234 if (c->msgsize == c->msgused) { 232 235 msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr)); … … 265 268 freetotal = 200; 266 269 freecurr = 0; 267 freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 268 /** TODO check for NULL **/ 270 if (!(freeconns = (conn **)malloc(sizeof(conn *) * freetotal))) { 271 perror("malloc()"); 272 } 269 273 return; 270 274 } … … 296 300 c->hdrsize = 0; 297 301 298 c->rbuf = (char *) malloc((size_t)c->rsize);299 c->wbuf = (char *) malloc((size_t)c->wsize);300 c->ilist = (item **) malloc(sizeof(item *) * c->isize);301 c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize);302 c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * c->msgsize);302 c->rbuf = (char *)malloc((size_t)c->rsize); 303 c->wbuf = (char *)malloc((size_t)c->wsize); 304 c->ilist = (item **)malloc(sizeof(item *) * c->isize); 305 c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); 306 c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize); 303 307 304 308 if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 || … … 372 376 373 377 static void conn_cleanup(conn *c) { 378 assert(c != NULL); 379 374 380 if (c->item) { 375 381 item_free(c->item); … … 411 417 412 418 static void conn_close(conn *c) { 419 assert(c != NULL); 420 413 421 /* delete the event, the socket and the conn */ 414 422 event_del(&c->event); … … 429 437 } else { 430 438 /* try to enlarge free connections array */ 431 conn **new_freeconns = realloc((void *)freeconns, sizeof(conn *)*freetotal*2);439 conn **new_freeconns = realloc((void *)freeconns, sizeof(conn *) * freetotal * 2); 432 440 if (new_freeconns) { 433 441 freetotal *= 2; … … 454 462 */ 455 463 static void conn_shrink(conn *c) { 464 assert(c != NULL); 465 456 466 if (c->udp) 457 467 return; … … 459 469 if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 460 470 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);471 memmove(c->rbuf, c->rcurr, (size_t)c->rbytes); 472 473 char *newbuf = (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE); 464 474 465 475 if (newbuf) { … … 472 482 473 483 if (c->isize > ITEM_LIST_HIGHWAT) { 474 item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0]));484 item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); 475 485 if (newbuf) { 476 486 c->ilist = newbuf; … … 481 491 482 492 if (c->msgsize > MSG_LIST_HIGHWAT) { 483 struct msghdr *newbuf = (struct msghdr *) realloc((void*)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0]));493 struct msghdr *newbuf = (struct msghdr *) realloc((void *)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); 484 494 if (newbuf) { 485 495 c->msglist = newbuf; … … 490 500 491 501 if (c->iovsize > IOV_LIST_HIGHWAT) { 492 struct iovec * newbuf = (struct iovec *) realloc((void*)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0]));502 struct iovec *newbuf = (struct iovec *) realloc((void *)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); 493 503 if (newbuf) { 494 504 c->iov = newbuf; … … 505 515 */ 506 516 static void conn_set_state(conn *c, int state) { 517 assert(c != NULL); 518 507 519 if (state != c->state) { 508 520 if (state == conn_read) { … … 522 534 */ 523 535 static int ensure_iov_space(conn *c) { 536 assert(c != NULL); 537 524 538 if (c->iovused >= c->iovsize) { 525 539 int i, iovnum; 526 struct iovec *new_iov = (struct iovec *) realloc(c->iov,540 struct iovec *new_iov = (struct iovec *)realloc(c->iov, 527 541 (c->iovsize * 2) * sizeof(struct iovec)); 528 542 if (! new_iov) … … 554 568 bool limit_to_mtu; 555 569 570 assert(c != NULL); 571 556 572 do { 557 573 m = &c->msglist[c->msgused - 1]; … … 582 598 583 599 m = &c->msglist[c->msgused - 1]; 584 m->msg_iov[m->msg_iovlen].iov_base = (void *)buf;600 m->msg_iov[m->msg_iovlen].iov_base = (void *)buf; 585 601 m->msg_iov[m->msg_iovlen].iov_len = len; 586 602 … … 603 619 int i; 604 620 unsigned char *hdr; 621 622 assert(c != NULL); 605 623 606 624 if (c->msgused > c->hdrsize) { … … 612 630 if (! new_hdrbuf) 613 631 return -1; 614 c->hdrbuf = (unsigned char *) new_hdrbuf;632 c->hdrbuf = (unsigned char *)new_hdrbuf; 615 633 c->hdrsize = c->msgused * 2; 616 634 } … … 628 646 *hdr++ = 0; 629 647 *hdr++ = 0; 630 assert((void *) hdr == (void*)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE);648 assert((void *) hdr == (void *)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); 631 649 } 632 650 … … 638 656 int len; 639 657 658 assert(c != NULL); 659 640 660 if (settings.verbose > 1) 641 661 fprintf(stderr, ">%d %s\n", c->sfd, str); 642 662 643 663 len = strlen(str); 644 if ( len + 2> c->wsize) {664 if ((len + 2) > c->wsize) { 645 665 /* ought to be always enough. just fail for simplicity */ 646 666 str = "SERVER_ERROR output line too long"; … … 664 684 665 685 static void complete_nread(conn *c) { 686 assert(c != NULL); 687 666 688 item *it = c->item; 667 689 int comm = c->item_comm; … … 719 741 720 742 typedef struct token_s { 721 char *value;743 char *value; 722 744 size_t length; 723 745 } token_t; … … 747 769 * } 748 770 */ 749 static size_t tokenize_command(char * command, token_t*tokens, const size_t max_tokens) {750 char *cp;751 char *value = NULL;771 static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) { 772 char *cp; 773 char *value = NULL; 752 774 size_t length = 0; 753 775 size_t ntokens = 0; … … 793 815 } 794 816 795 static void process_stat(conn *c, token_t *tokens, const size_t ntokens) {817 static void process_stat(conn *c, token_t *tokens, const size_t ntokens) { 796 818 rel_time_t now = current_time; 797 char* command; 798 char* subcommand; 819 char *command; 820 char *subcommand; 821 822 assert(c != NULL); 799 823 800 824 if(ntokens < 2) { … … 817 841 pos += sprintf(pos, "STAT time %ld\r\n", now + stats.started); 818 842 pos += sprintf(pos, "STAT version " VERSION "\r\n"); 819 pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *));843 pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *)); 820 844 pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec); 821 845 pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); … … 833 857 pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read); 834 858 pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written); 835 pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long) settings.maxbytes);859 pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long)settings.maxbytes); 836 860 pos += sprintf(pos, "END"); 837 861 out_string(c, temp); … … 877 901 int res; 878 902 879 wbuf = (char *)malloc(wsize); 880 if (wbuf == 0) { 903 if (!(wbuf = (char *)malloc(wsize))) { 881 904 out_string(c, "SERVER_ERROR out of memory"); 882 905 return; … … 902 925 } 903 926 strcpy(wbuf + res, "END\r\n"); 904 c->write_and_free =wbuf;905 c->wcurr =wbuf;927 c->write_and_free = wbuf; 928 c->wcurr = wbuf; 906 929 c->wbytes = res + 5; // Don't write the terminal '\0' 907 930 conn_set_state(c, conn_write); … … 943 966 } 944 967 945 if (strcmp(subcommand, "slabs") ==0) {968 if (strcmp(subcommand, "slabs") == 0) { 946 969 int bytes = 0; 947 970 char *buf = slabs_stats(&bytes); … … 958 981 } 959 982 960 if (strcmp(subcommand, "items") ==0) {983 if (strcmp(subcommand, "items") == 0) { 961 984 char buffer[4096]; 962 985 item_stats(buffer, 4096); … … 965 988 } 966 989 967 if (strcmp(subcommand, "sizes") ==0) {990 if (strcmp(subcommand, "sizes") == 0) { 968 991 int bytes = 0; 969 992 char *buf = item_stats_sizes(&bytes); … … 985 1008 986 1009 /* ntokens is overwritten here... shrug.. */ 987 static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) {1010 static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) { 988 1011 char *key; 989 1012 size_t nkey; 990 1013 int i = 0; 991 1014 item *it; 992 token_t* key_token = &tokens[KEY_TOKEN]; 1015 token_t *key_token = &tokens[KEY_TOKEN]; 1016 1017 assert(c != NULL); 993 1018 994 1019 if (settings.managed) { … … 1020 1045 if (it) { 1021 1046 if (i >= c->isize) { 1022 item **new_list = realloc(c->ilist, sizeof(item *) *c->isize*2);1047 item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2); 1023 1048 if (new_list) { 1024 1049 c->isize *= 2; … … 1082 1107 } 1083 1108 1084 static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm) {1109 static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm) { 1085 1110 char *key; 1086 1111 size_t nkey; … … 1089 1114 int vlen; 1090 1115 item *it; 1116 1117 assert(c != NULL); 1091 1118 1092 1119 if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { … … 1129 1156 /* swallow the data line */ 1130 1157 c->write_and_go = conn_swallow; 1131 c->sbytes = vlen +2;1158 c->sbytes = vlen + 2; 1132 1159 return; 1133 1160 } … … 1141 1168 } 1142 1169 1143 static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) {1170 static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) { 1144 1171 char temp[32]; 1145 1172 unsigned int value; … … 1150 1177 int res; 1151 1178 char *ptr; 1179 1180 assert(c != NULL); 1152 1181 1153 1182 if(tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { … … 1186 1215 1187 1216 ptr = ITEM_data(it); 1188 while ((*ptr != '\0') && (*ptr <'0' && *ptr>'9')) ptr++; // BUG: can't be true1217 while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++; // BUG: can't be true 1189 1218 1190 1219 value = strtol(ptr, NULL, 10); … … 1196 1225 1197 1226 if (incr != 0) 1198 value +=delta;1227 value += delta; 1199 1228 else { 1200 1229 if (delta >= value) value = 0; 1201 else value -=delta;1230 else value -= delta; 1202 1231 } 1203 1232 snprintf(temp, 32, "%u", value); … … 1215 1244 } else { /* replace in-place */ 1216 1245 memcpy(ITEM_data(it), temp, res); 1217 memset(ITEM_data(it) + res, ' ', it->nbytes -res-2);1246 memset(ITEM_data(it) + res, ' ', it->nbytes - res - 2); 1218 1247 } 1219 1248 out_string(c, temp); … … 1221 1250 } 1222 1251 1223 static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) {1252 static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) { 1224 1253 char *key; 1225 1254 size_t nkey; … … 1227 1256 time_t exptime = 0; 1228 1257 1258 assert(c != NULL); 1259 1229 1260 if (settings.managed) { 1230 1261 int bucket = c->bucket; … … 1298 1329 int comm; 1299 1330 1331 assert(c != NULL); 1332 1300 1333 if (settings.verbose > 1) 1301 1334 fprintf(stderr, "<%d %s\n", c->sfd, command); … … 1387 1420 return; 1388 1421 } 1389 if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) {1422 if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) { 1390 1423 /* we never write anything back, even if input's wrong */ 1391 if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <=0)) {1424 if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <= 0)) { 1392 1425 /* do nothing, bad input */ 1393 1426 } else { … … 1479 1512 char *el, *cont; 1480 1513 1481 assert(c->rcurr <= c->rbuf + c->rsize); 1514 assert(c != NULL); 1515 assert(c->rcurr <= (c->rbuf + c->rsize)); 1482 1516 1483 1517 if (c->rbytes == 0) … … 1487 1521 return 0; 1488 1522 cont = el + 1; 1489 if ( el - c->rcurr> 1 && *(el - 1) == '\r') {1523 if ((el - c->rcurr) > 1 && *(el - 1) == '\r') { 1490 1524 el--; 1491 1525 } 1492 1526 *el = '\0'; 1493 1527 1494 assert(cont <= c->rcurr + c->rbytes);1528 assert(cont <= (c->rcurr + c->rbytes)); 1495 1529 1496 1530 process_command(c, c->rcurr); … … 1499 1533 c->rcurr = cont; 1500 1534 1501 assert(c->rcurr <= c->rbuf + c->rsize);1535 assert(c->rcurr <= (c->rbuf + c->rsize)); 1502 1536 1503 1537 return 1; … … 1510 1544 static int try_read_udp(conn *c) { 1511 1545 int res; 1546 1547 assert(c != NULL); 1512 1548 1513 1549 c->request_addr_size = sizeof(c->request_addr); … … 1549 1585 int res; 1550 1586 1587 assert(c != NULL); 1588 1551 1589 if (c->rcurr != c->rbuf) { 1552 1590 if (c->rbytes != 0) /* otherwise there's nothing to copy */ … … 1557 1595 while (1) { 1558 1596 if (c->rbytes >= c->rsize) { 1559 char *new_rbuf = realloc(c->rbuf, c->rsize *2);1597 char *new_rbuf = realloc(c->rbuf, c->rsize * 2); 1560 1598 if (!new_rbuf) { 1561 1599 if (settings.verbose > 0) … … 1566 1604 return 1; 1567 1605 } 1568 c->rcurr = c->rbuf = new_rbuf;1606 c->rcurr = c->rbuf = new_rbuf; 1569 1607 c->rsize *= 2; 1570 1608 } … … 1600 1638 1601 1639 static bool update_event(conn *c, const int new_flags) { 1640 assert(c != NULL); 1641 1602 1642 if (c->ev_flags == new_flags) 1603 1643 return true; … … 1640 1680 int res; 1641 1681 1682 assert(c != NULL); 1683 1642 1684 if (c->msgcurr < c->msgused && 1643 1685 c->msglist[c->msgcurr].msg_iovlen == 0) { … … 1696 1738 socklen_t addrlen; 1697 1739 struct sockaddr addr; 1698 conn *newc;1699 1740 int res; 1741 1742 assert(c != NULL); 1700 1743 1701 1744 while (!stop) { … … 1722 1765 break; 1723 1766 } 1724 newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST,1767 conn *newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 1725 1768 DATA_BUFFER_SIZE, false); 1726 1769 if (!newc) { … … 1909 1952 1910 1953 c = (conn *)arg; 1954 assert(c != NULL); 1955 1911 1956 c->which = which; 1912 1957 … … 1966 2011 1967 2012 while (min <= max) { 1968 avg = ((unsigned int) min + max) / 2;2013 avg = ((unsigned int)(min + max)) / 2; 1969 2014 if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &avg, intsize) == 0) { 1970 2015 last_good = avg; … … 2008 2053 addr.sin_port = htons(port); 2009 2054 addr.sin_addr = settings.interface; 2010 if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {2055 if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 2011 2056 perror("bind()"); 2012 2057 close(sfd); … … 2074 2119 addr.sun_family = AF_UNIX; 2075 2120 strcpy(addr.sun_path, path); 2076 if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {2121 if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 2077 2122 perror("bind()"); 2078 2123 close(sfd); … … 2088 2133 2089 2134 /* listening socket */ 2090 static int l_socket =0;2135 static int l_socket = 0; 2091 2136 2092 2137 /* udp socket */ 2093 static int u_socket =-1;2138 static int u_socket = -1; 2094 2139 2095 2140 /* invoke right before gdb is called, on assert */ 2096 2141 void pre_gdb(void) { 2097 int i = 0;2142 int i; 2098 2143 if (l_socket > -1) close(l_socket); 2099 2144 if (u_socket > -1) close(u_socket); 2100 for (i =3; i<=500; i++) close(i); /* so lame */2145 for (i = 3; i <= 500; i++) close(i); /* so lame */ 2101 2146 kill(getpid(), SIGABRT); 2102 2147 } … … 2268 2313 return; 2269 2314 2270 if (!(fp = fopen(pid_file, "w"))) {2271 fprintf(stderr, "Could not open the pid file %s for writing\n",pid_file);2315 if (!(fp = fopen(pid_file, "w"))) { 2316 fprintf(stderr, "Could not open the pid file %s for writing\n", pid_file); 2272 2317 return; 2273 2318 } 2274 2319 2275 fprintf(fp,"%ld\n", (long)pid);2320 fprintf(fp,"%ld\n", (long)pid); 2276 2321 if (fclose(fp) == -1) { 2277 fprintf(stderr, "Could not close the pid file %s.\n",pid_file);2322 fprintf(stderr, "Could not close the pid file %s.\n", pid_file); 2278 2323 return; 2279 2324 } … … 2285 2330 2286 2331 if (unlink(pid_file) != 0) { 2287 fprintf(stderr, "Could not remove the pid file %s.\n",pid_file);2332 fprintf(stderr, "Could not remove the pid file %s.\n", pid_file); 2288 2333 } 2289 2334 … … 2334 2379 break; 2335 2380 case 'm': 2336 settings.maxbytes = ((size_t)atoi(optarg)) *1024*1024;2381 settings.maxbytes = ((size_t)atoi(optarg)) * 1024 * 1024; 2337 2382 break; 2338 2383 case 'M': … … 2400 2445 * the soft limit to the hard. 2401 2446 */ 2402 if (getrlimit(RLIMIT_CORE, &rlim) ==0) {2447 if (getrlimit(RLIMIT_CORE, &rlim) == 0) { 2403 2448 rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY; 2404 if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) {2449 if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) { 2405 2450 /* failed. try raising just to the old max */ 2406 rlim_new.rlim_cur = rlim_new.rlim_max = 2407 rlim.rlim_max; 2408 (void) setrlimit(RLIMIT_CORE, &rlim_new); 2451 rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max; 2452 (void)setrlimit(RLIMIT_CORE, &rlim_new); 2409 2453 } 2410 2454 } … … 2415 2459 */ 2416 2460 2417 if ((getrlimit(RLIMIT_CORE, &rlim) !=0) || rlim.rlim_cur==0) {2461 if ((getrlimit(RLIMIT_CORE, &rlim) != 0) || rlim.rlim_cur == 0) { 2418 2462 fprintf(stderr, "failed to ensure corefile creation\n"); 2419 2463 exit(EXIT_FAILURE); … … 2467 2511 2468 2512 /* lose root privileges if we have them */ 2469 if (getuid() == 0 || geteuid()==0) {2470 if (username ==0 || *username=='\0') {2513 if (getuid() == 0 || geteuid() == 0) { 2514 if (username == 0 || *username == '\0') { 2471 2515 fprintf(stderr, "can't run as root without the -u switch\n"); 2472 2516 return 1; … … 2476 2520 return 1; 2477 2521 } 2478 if (setgid(pw->pw_gid) <0 || setuid(pw->pw_uid)<0) {2522 if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { 2479 2523 fprintf(stderr, "failed to assume identity of user %s\n", username); 2480 2524 return 1; … … 2513 2557 /* managed instance? alloc and zero a bucket array */ 2514 2558 if (settings.managed) { 2515 buckets = malloc(sizeof(int) *MAX_BUCKETS);2559 buckets = malloc(sizeof(int) * MAX_BUCKETS); 2516 2560 if (buckets == 0) { 2517 2561 fprintf(stderr, "failed to allocate the bucket array"); 2518 2562 exit(EXIT_FAILURE); 2519 2563 } 2520 memset(buckets, 0, sizeof(int) *MAX_BUCKETS);2564 memset(buckets, 0, sizeof(int) * MAX_BUCKETS); 2521 2565 } 2522 2566 … … 2553 2597 } 2554 2598 /* initialise clock event */ 2555 clock_handler(0, 0,0);2599 clock_handler(0, 0, 0); 2556 2600 /* initialise deletion array and timer event */ 2557 deltotal = 200; delcurr = 0; 2558 todelete = malloc(sizeof(item *)*deltotal); 2559 delete_handler(0,0,0); /* sets up the event */ 2601 deltotal = 200; 2602 delcurr = 0; 2603 todelete = malloc(sizeof(item *) * deltotal); 2604 delete_handler(0, 0, 0); /* sets up the event */ 2560 2605 /* save the PID in if we're a daemon */ 2561 2606 if (daemonize) 2562 save_pid(getpid(), pid_file);2607 save_pid(getpid(), pid_file); 2563 2608 /* enter the loop */ 2564 2609 event_loop(0); trunk/server/slabs.c
r468 r489 55 55 } slabclass_t; 56 56 57 static slabclass_t slabclass[POWER_LARGEST +1];57 static slabclass_t slabclass[POWER_LARGEST + 1]; 58 58 static size_t mem_limit = 0; 59 59 static size_t mem_malloced = 0; … … 65 65 static int slabs_newslab(const unsigned int id); 66 66 67 #ifndef DONT_PREALLOC_SLABS 67 68 /* Preallocate as many slab pages as possible (called from slabs_init) 68 69 on start-up, so users don't get confused out-of-memory errors when … … 72 73 smaller ones will be made. */ 73 74 static void slabs_preallocate (const unsigned int maxslabs); 74 75 #endif 75 76 76 77 /* … … 85 86 int res = POWER_SMALLEST; 86 87 87 if(size ==0)88 if(size == 0) 88 89 return 0; 89 90 while (size > slabclass[res].size) … … 130 131 char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC"); 131 132 if (t_initial_malloc) { 132 mem_malloced = (size_t) atol(t_initial_malloc);133 mem_malloced = (size_t)atol(t_initial_malloc); 133 134 } 134 135 … … 146 147 } 147 148 149 #ifndef DONT_PREALLOC_SLABS 148 150 static void slabs_preallocate (const unsigned int maxslabs) { 149 151 int i; … … 156 158 these three lines. */ 157 159 158 for (i=POWER_SMALLEST; i<=POWER_LARGEST; i++) {160 for (i = POWER_SMALLEST; i <= POWER_LARGEST; i++) { 159 161 if (++prealloc > maxslabs) 160 162 return; … … 163 165 164 166 } 167 #endif 165 168 166 169 static int grow_slab_list (const unsigned int id) { … … 168 171 if (p->slabs == p->list_size) { 169 172 size_t new_size = (p->list_size != 0) ? p->list_size * 2 : 16; 170 void *new_list = realloc(p->slab_list, new_size *sizeof(void*));173 void *new_list = realloc(p->slab_list, new_size * sizeof(void *)); 171 174 if (new_list == 0) return 0; 172 175 p->list_size = new_size; … … 211 214 212 215 p = &slabclass[id]; 213 assert(p->sl_curr == 0 || ((item *)p->slots[p->sl_curr-1])->slabs_clsid == 0);216 assert(p->sl_curr == 0 || ((item *)p->slots[p->sl_curr - 1])->slabs_clsid == 0); 214 217 215 218 #ifdef USE_SYSTEM_MALLOC … … 222 225 /* fail unless we have space at the end of a recently allocated page, 223 226 we have something on our freelist, or we could allocate a new page */ 224 if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || slabs_newslab(id) !=0))227 if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || slabs_newslab(id) != 0)) 225 228 return 0; 226 229 … … 247 250 slabclass_t *p; 248 251 249 assert(((item *)ptr)->slabs_clsid ==0);252 assert(((item *)ptr)->slabs_clsid == 0); 250 253 assert(id >= POWER_SMALLEST && id <= power_largest); 251 254 if (id < POWER_SMALLEST || id > power_largest) … … 261 264 262 265 if (p->sl_curr == p->sl_total) { /* need more space on the free list */ 263 int new_size = (p->sl_total != 0) ? p->sl_total *2 : 16; /* 16 is arbitrary */264 void **new_slots = realloc(p->slots, new_size *sizeof(void *));266 int new_size = (p->sl_total != 0) ? p->sl_total * 2 : 16; /* 16 is arbitrary */ 267 void **new_slots = realloc(p->slots, new_size * sizeof(void *)); 265 268 if (new_slots == 0) 266 269 return; … … 275 278 char* slabs_stats(int *buflen) { 276 279 int i, total; 277 char *buf = (char *)malloc(power_largest * 200 + 100);280 char *buf = (char *)malloc(power_largest * 200 + 100); 278 281 char *bufcurr = buf; 279 282 … … 300 303 } 301 304 } 302 bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long) mem_malloced);305 bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long)mem_malloced); 303 306 bufcurr += sprintf(bufcurr, "END\r\n"); 304 307 *buflen = bufcurr - buf; … … 338 341 if (p->killing == 0) p->killing = 1; 339 342 340 slab = p->slab_list[p->killing -1];343 slab = p->slab_list[p->killing - 1]; 341 344 slab_end = slab + POWER_BLOCK; 342 345 343 for (iter =slab; iter<slab_end; iter+=p->size) {344 item *it = (item *) iter;346 for (iter = slab; iter < slab_end; iter += p->size) { 347 item *it = (item *)iter; 345 348 if (it->slabs_clsid) { 346 349 if (it->refcount) was_busy = 1; … … 352 355 { 353 356 int fi; 354 for (fi =p->sl_curr-1; fi>=0; fi--) {357 for (fi = p->sl_curr - 1; fi >= 0; fi--) { 355 358 if (p->slots[fi] >= slab && p->slots[fi] < slab_end) { 356 359 p->sl_curr--; … … 363 366 364 367 /* if good, now move it to the dst slab class */ 365 p->slab_list[p->killing -1] = p->slab_list[p->slabs-1];368 p->slab_list[p->killing - 1] = p->slab_list[p->slabs - 1]; 366 369 p->slabs--; 367 370 p->killing = 0; … … 371 374 /* this isn't too critical, but other parts of the code do asserts to 372 375 make sure this field is always 0. */ 373 for (iter =slab; iter<slab_end; iter+=dp->size) {376 for (iter = slab; iter < slab_end; iter += dp->size) { 374 377 ((item *)iter)->slabs_clsid = 0; 375 378 } trunk/server/t/stats.t
r476 r489 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";
