Show
Ignore:
Timestamp:
08/24/09 04:55:36 (5 years ago)
Author:
kazuho
Message:

update picoev

Location:
platform/mysql/mycached/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • platform/mysql/mycached/trunk/picoev.h

    r34991 r35002  
     1/* 
     2 * Copyright (c) 2009, Cybozu Labs, Inc. 
     3 * All rights reserved. 
     4 *  
     5 * Redistribution and use in source and binary forms, with or without 
     6 * modification, are permitted provided that the following conditions are met: 
     7 *  
     8 * * Redistributions of source code must retain the above copyright notice, 
     9 *   this list of conditions and the following disclaimer. 
     10 * * Redistributions in binary form must reproduce the above copyright notice, 
     11 *   this list of conditions and the following disclaimer in the documentation 
     12 *   and/or other materials provided with the distribution. 
     13 * * Neither the name of the <ORGANIZATION> nor the names of its contributors 
     14 *   may be used to endorse or promote products derived from this software 
     15 *   without specific prior written permission. 
     16 *  
     17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
     18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
     19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
     20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
     21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
     22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
     23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
     24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
     25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
     27 * POSSIBILITY OF SUCH DAMAGE. 
     28 */ 
     29 
    130#ifndef picoev_h 
    231#define picoev_h 
     
    1645 
    1746#define PICOEV_IS_INITED (picoev.max_fd != 0)   
    18 #define PICOEV_IS_INITED_AND_FD_IN_RANGE(fd) ((fd) < picoev.max_fd) 
     47#define PICOEV_IS_INITED_AND_FD_IN_RANGE(fd) \ 
     48  (((unsigned)fd) < (unsigned)picoev.max_fd) 
    1949#define PICOEV_TOO_MANY_LOOPS (picoev.num_loops != 0) /* use after ++ */ 
    2050#define PICOEV_FD_BELONGS_TO_LOOP(loop, fd) \ 
     
    2959#define PICOEV_SIMD_BITS 128 
    3060#define PICOEV_TIMEOUT_VEC_SIZE 128 
    31 #define PICOEV_LONG_BITS (sizeof(long) * 8) 
     61#define PICOEV_SHORT_BITS (sizeof(short) * 8) 
    3262 
    3363#define PICOEV_READ 1 
     
    5686    picoev_loop_id_t loop_id; 
    5787    struct { 
    58       long* vec; 
    59       long* vec_of_vec; 
     88      short* vec; 
     89      short* vec_of_vec; 
    6090      size_t base_idx; 
    6191      time_t base_time; 
     
    87117    picoev.num_loops = 0; 
    88118    picoev.timeout_vec_size 
    89       = PICOEV_RND_UP(picoev.max_fd, PICOEV_SIMD_BITS) / PICOEV_LONG_BITS; 
     119      = PICOEV_RND_UP(picoev.max_fd, PICOEV_SIMD_BITS) / PICOEV_SHORT_BITS; 
    90120    picoev.timeout_vec_of_vec_size 
    91121      = PICOEV_RND_UP(picoev.timeout_vec_size, PICOEV_SIMD_BITS) 
    92       / PICOEV_LONG_BITS; 
     122      / PICOEV_SHORT_BITS; 
    93123    return 0; 
    94124  } 
     
    120150  void picoev_set_timeout(picoev_loop* loop, int fd, int secs) { 
    121151    picoev_fd* target; 
    122     long* vec, * vec_of_vec; 
    123     size_t vi = fd / PICOEV_LONG_BITS, delta; 
     152    short* vec, * vec_of_vec; 
     153    size_t vi = fd / PICOEV_SHORT_BITS, delta; 
    124154    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd)); 
    125155    assert(PICOEV_FD_BELONGS_TO_LOOP(loop, fd)); 
     
    128158    if (target->timeout_idx != -1) { 
    129159      vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx); 
    130       if ((vec[vi] &= ~((unsigned long)LONG_MIN >> (fd % PICOEV_LONG_BITS))) 
     160      if ((vec[vi] &= ~((unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS))) 
    131161          == 0) { 
    132162        vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx); 
    133         vec_of_vec[vi / PICOEV_LONG_BITS] 
    134           &= ~((unsigned long)LONG_MIN >> (vi % PICOEV_LONG_BITS)); 
     163        vec_of_vec[vi / PICOEV_SHORT_BITS] 
     164          &= ~((unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS)); 
    135165      } 
    136166      target->timeout_idx = -1; 
     
    145175        (loop->timeout.base_idx + delta) % PICOEV_TIMEOUT_VEC_SIZE; 
    146176      vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx); 
    147       vec[vi] |= (unsigned long)LONG_MIN >> (fd % PICOEV_LONG_BITS); 
     177      vec[vi] |= (unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS); 
    148178      vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx); 
    149       vec_of_vec[vi / PICOEV_LONG_BITS] 
    150         |= (unsigned long)LONG_MIN >> (vi % PICOEV_LONG_BITS); 
     179      vec_of_vec[vi / PICOEV_SHORT_BITS] 
     180        |= (unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS); 
    151181    } 
    152182  } 
     
    188218  } 
    189219   
     220  /* check if fd is registered (checks all loops if loop == NULL) */ 
     221  PICOEV_INLINE 
     222  int picoev_is_active(picoev_loop* loop, int fd) { 
     223    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd)); 
     224    return loop != NULL 
     225      ? picoev.fds[fd].loop_id == loop->loop_id 
     226      : picoev.fds[fd].loop_id != 0; 
     227  } 
     228   
    190229  /* returns events being watched for given descriptor */ 
    191230  PICOEV_INLINE 
     
    213252    /* TODO uses valloc to align memory, for future optimisation using SIMD */ 
    214253    if ((loop->timeout.vec 
    215          = (long*)valloc((picoev.timeout_vec_size 
     254         = (short*)valloc((picoev.timeout_vec_size 
    216255                          + picoev.timeout_vec_of_vec_size) 
    217                          * sizeof(long) * PICOEV_TIMEOUT_VEC_SIZE)) 
     256                         * sizeof(short) * PICOEV_TIMEOUT_VEC_SIZE)) 
    218257        == NULL) { 
    219258      --picoev.num_loops; 
     
    240279  void picoev_handle_timeout_internal(picoev_loop* loop) { 
    241280    size_t i, j, k; 
    242     loop->now = time(NULL); 
    243281    for (; 
    244282         loop->timeout.base_time <= loop->now - loop->timeout.resolution;  
     
    247285           loop->timeout.base_time += loop->timeout.resolution) { 
    248286      /* TODO use SIMD instructions */ 
    249       long* vec = PICOEV_TIMEOUT_VEC_OF(loop, loop->timeout.base_idx); 
    250       long* vec_of_vec 
     287      short* vec = PICOEV_TIMEOUT_VEC_OF(loop, loop->timeout.base_idx); 
     288      short* vec_of_vec 
    251289        = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, loop->timeout.base_idx); 
    252290      for (i = 0; i < picoev.timeout_vec_of_vec_size; ++i) { 
    253         long vv = vec_of_vec[i]; 
     291        short vv = vec_of_vec[i]; 
    254292        if (vv != 0) { 
    255           for (j = i * PICOEV_LONG_BITS; vv != 0; j++, vv <<= 1) { 
     293          for (j = i * PICOEV_SHORT_BITS; vv != 0; j++, vv <<= 1) { 
    256294            if (vv < 0) { 
    257               long v = vec[j]; 
     295              short v = vec[j]; 
    258296              assert(v != 0); 
    259               for (k = j * PICOEV_LONG_BITS; v != 0; k++, v <<= 1) { 
     297              for (k = j * PICOEV_SHORT_BITS; v != 0; k++, v <<= 1) { 
    260298                if (v < 0) { 
    261299                  picoev_fd* fd = picoev.fds + k; 
     
    277315  int picoev_loop_once(picoev_loop* loop, int max_wait) { 
    278316    loop->now = time(NULL); 
     317    if (max_wait > loop->timeout.resolution) { 
     318      max_wait = loop->timeout.resolution; 
     319    } 
    279320    if (picoev_poll_once_internal(loop, max_wait) != 0) { 
    280321      return -1; 
     322    } 
     323    if (max_wait != 0) { 
     324      loop->now = time(NULL); 
    281325    } 
    282326    picoev_handle_timeout_internal(loop); 
  • platform/mysql/mycached/trunk/picoev_epoll.c

    r34991 r35002  
     1/* 
     2 * Copyright (c) 2009, Cybozu Labs, Inc. 
     3 * All rights reserved. 
     4 *  
     5 * Redistribution and use in source and binary forms, with or without 
     6 * modification, are permitted provided that the following conditions are met: 
     7 *  
     8 * * Redistributions of source code must retain the above copyright notice, 
     9 *   this list of conditions and the following disclaimer. 
     10 * * Redistributions in binary form must reproduce the above copyright notice, 
     11 *   this list of conditions and the following disclaimer in the documentation 
     12 *   and/or other materials provided with the distribution. 
     13 * * Neither the name of the <ORGANIZATION> nor the names of its contributors 
     14 *   may be used to endorse or promote products derived from this software 
     15 *   without specific prior written permission. 
     16 *  
     17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
     18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
     19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
     20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
     21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
     22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
     23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
     24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
     25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
     27 * POSSIBILITY OF SUCH DAMAGE. 
     28 */ 
     29 
    130#include <sys/epoll.h> 
    231#include <unistd.h> 
     
    82111{ 
    83112  picoev_loop_epoll* loop = (picoev_loop_epoll*)_loop; 
    84   int i, nevents, timeout_secs; 
     113  int i, nevents; 
    85114   
    86   timeout_secs = loop->loop.timeout.resolution; 
    87   if (max_wait != 0 && max_wait < timeout_secs) { 
    88     timeout_secs = max_wait; 
    89   } 
    90115  nevents = epoll_wait(loop->epfd, loop->events, 
    91116                       sizeof(loop->events) / sizeof(loop->events[0]), 
    92                        timeout_secs * 1000); 
     117                       max_wait * 1000); 
    93118  if (nevents == -1) { 
    94119    return -1; 
  • platform/mysql/mycached/trunk/picoev_kqueue.c

    r34991 r35002  
     1/* 
     2 * Copyright (c) 2009, Cybozu Labs, Inc. 
     3 * All rights reserved. 
     4 *  
     5 * Redistribution and use in source and binary forms, with or without 
     6 * modification, are permitted provided that the following conditions are met: 
     7 *  
     8 * * Redistributions of source code must retain the above copyright notice, 
     9 *   this list of conditions and the following disclaimer. 
     10 * * Redistributions in binary form must reproduce the above copyright notice, 
     11 *   this list of conditions and the following disclaimer in the documentation 
     12 *   and/or other materials provided with the distribution. 
     13 * * Neither the name of the <ORGANIZATION> nor the names of its contributors 
     14 *   may be used to endorse or promote products derived from this software 
     15 *   without specific prior written permission. 
     16 *  
     17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
     18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
     19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
     20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
     21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
     22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
     23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
     24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
     25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
     27 * POSSIBILITY OF SUCH DAMAGE. 
     28 */ 
     29 
    130#include <errno.h> 
    231#include <sys/types.h> 
     
    3160   
    3261  if (picoev.fds[fd].events != 0) { 
    33     SET(picoev.fds[fd].events, EV_DISABLE); 
     62    SET(picoev.fds[fd].events, EV_ADD | EV_ENABLE); 
    3463  } 
    3564  if (events != 0) { 
     
    93122  int nevents, i; 
    94123   
    95   ts.tv_sec = loop->loop.timeout.resolution; 
    96   if (max_wait != 0 && max_wait < ts.tv_sec) { 
    97     ts.tv_sec = max_wait; 
    98   } 
     124  ts.tv_sec = max_wait; 
    99125  ts.tv_nsec = 0; 
    100    
    101126  nevents = kevent(loop->kq, loop->ev_queue, loop->ev_queue_off, loop->events, 
    102127                   sizeof(loop->events) / sizeof(loop->events[0]), &ts); 
     128  loop->ev_queue_off = 0; 
    103129  if (nevents == -1) { 
    104130    /* the errors we can only rescue */ 
     
    120146  return 0; 
    121147} 
    122    
  • platform/mysql/mycached/trunk/picoev_select.c

    r34991 r35002  
     1/* 
     2 * Copyright (c) 2009, Cybozu Labs, Inc. 
     3 * All rights reserved. 
     4 *  
     5 * Redistribution and use in source and binary forms, with or without 
     6 * modification, are permitted provided that the following conditions are met: 
     7 *  
     8 * * Redistributions of source code must retain the above copyright notice, 
     9 *   this list of conditions and the following disclaimer. 
     10 * * Redistributions in binary form must reproduce the above copyright notice, 
     11 *   this list of conditions and the following disclaimer in the documentation 
     12 *   and/or other materials provided with the distribution. 
     13 * * Neither the name of the <ORGANIZATION> nor the names of its contributors 
     14 *   may be used to endorse or promote products derived from this software 
     15 *   without specific prior written permission. 
     16 *  
     17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
     18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
     19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
     20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
     21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
     22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
     23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
     24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
     25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
     27 * POSSIBILITY OF SUCH DAMAGE. 
     28 */ 
     29 
    130#include <sys/select.h> 
    231#include "picoev.h" 
     
    6291   
    6392  /* select and handle if any */ 
    64   tv.tv_sec = loop->timeout.resolution; 
    65   if (max_wait != 0 && max_wait < tv.tv_sec) { 
    66     tv.tv_sec = max_wait; 
    67   } 
     93  tv.tv_sec = max_wait; 
    6894  tv.tv_usec = 0; 
    6995  r = select(maxfd + 1, &readfds, &writefds, &errorfds, &tv);