Commit 798c521d authored by Sam Rushing's avatar Sam Rushing

merge with master

parents b0d96be7 554c2433
# Copyright (c) 2002-2011 IronPort Systems and Cisco Systems
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
......@@ -214,6 +214,11 @@ cdef extern from "sys/sysctl.h":
int sysctlbyname(char *name, void *oldp, size_t *oldlenp, void *newp,
size_t newlen)
IF UNAME_SYSNAME == "Linux":
DEF _GNU_SOURCE=1
cdef extern from "sched.h":
int sched_getcpu()
# This is a pointer so that the time shifting functions can replace it.
cdef uint64_t (*c_rdtsc) ()
c_rdtsc = _c_rdtsc
......@@ -241,9 +246,34 @@ cdef uint64_t get_ticks_per_sec() except -1:
cdef char buffer[128]
cdef size_t buffer_size
# XXX - Need to find a way to get ticks per sec on Linux, fake it for now
IF UNAME_SYSNAME == "Linux":
return 2793008320
current_cpu_number = sched_getcpu()
f = open('/proc/cpuinfo')
try:
lines = f.readlines()
finally:
f.close()
cpu_found = False
cpu_speed_mhz = None
for line in lines:
if line.startswith('processor'):
found_cpu_number = int(line.split(':')[-1].strip())
if found_cpu_number == current_cpu_number:
cpu_found = True
continue
if not cpu_found:
continue
if line.startswith('cpu MHz'):
cpu_speed_mhz = float(line.split(':')[-1].strip())
break
if cpu_speed_mhz is None:
raise RuntimeError('failed to detect CPU frequency')
return cpu_speed_mhz * 1000000
buffer_size = sizeof(buffer)
......
......@@ -25,6 +25,8 @@ __socket_version__ = "$Id: //prod/main/ap/shrapnel/coro/socket.pyx#57 $"
# Note: this file is included by <coro.pyx>
DEF KQUEUE = (UNAME_SYSNAME == "FreeBSD" or UNAME_SYSNAME == "Darwin")
# ================================================================================
# socket
# ================================================================================
......@@ -387,12 +389,13 @@ cdef public class sock [ object sock_object, type sock_type ]:
"""
cdef bytes buffer
cdef int r, new_buffer_size
cdef char * p
buffer = PyBytes_FromStringAndSize (NULL, buffer_size)
p = buffer
while 1:
if self._try_selfish() == 1:
#r = recv (self.fd, buffer, buffer_size, 0)
r = read (self.fd, buffer, buffer_size)
r = read (self.fd, p, buffer_size)
else:
r = -1
errno.errno = errno.EAGAIN
......@@ -400,9 +403,12 @@ cdef public class sock [ object sock_object, type sock_type ]:
if errno.errno == errno.EAGAIN:
# kqueue will tell us exactly how many bytes are waiting for us.
new_buffer_size = min (self._wait_for_read(), buffer_size)
if new_buffer_size != buffer_size:
buffer = PyBytes_FromStringAndSize (NULL, new_buffer_size)
buffer_size = new_buffer_size
IF KQUEUE:
# kqueue will tell us exactly how many bytes are waiting for us.
if new_buffer_size != buffer_size:
buffer = PyBytes_FromStringAndSize (NULL, new_buffer_size)
buffer_size = new_buffer_size
p = buffer
else:
raise_oserror()
elif r == 0:
......@@ -441,13 +447,15 @@ cdef public class sock [ object sock_object, type sock_type ]:
cdef sockaddr_storage sa
cdef int r, new_buffer_size
cdef socklen_t addr_len
cdef char * p
buffer = PyBytes_FromStringAndSize (NULL, buffer_size)
p = buffer
while 1:
if self._try_selfish() == 1:
addr_len = sizeof (sockaddr_storage)
memset (&sa, 0, sizeof (sockaddr_storage))
r = recvfrom (self.fd, <void*>buffer, buffer_size, flags, <sockaddr*>&sa, &addr_len)
r = recvfrom (self.fd, <void*>p, buffer_size, flags, <sockaddr*>&sa, &addr_len)
else:
r = -1
errno.errno = errno.EAGAIN
......@@ -455,9 +463,12 @@ cdef public class sock [ object sock_object, type sock_type ]:
if errno.errno == errno.EAGAIN:
# kqueue will tell us exactly how many bytes are waiting for us.
new_buffer_size = min (self._wait_for_read(), buffer_size)
if new_buffer_size != buffer_size:
buffer = PyBytes_FromStringAndSize (NULL, new_buffer_size)
buffer_size = new_buffer_size
IF KQUEUE:
# kqueue will tell us exactly how many bytes are waiting for us.
if new_buffer_size != buffer_size:
buffer = PyBytes_FromStringAndSize (NULL, new_buffer_size)
buffer_size = new_buffer_size
p = buffer
else:
raise_oserror()
else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment