Skip to content

Commit

Permalink
Solaris/Sparc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto@solaris committed Apr 19, 2010
1 parent 688ffe3 commit 47847ee
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 25 deletions.
7 changes: 5 additions & 2 deletions async.c
Expand Up @@ -101,13 +101,15 @@ int async_queue_init(int serverfd) {
int dpfd ;
struct pollfd dpev;


dpfd = open("/dev/poll", O_RDWR);

if (dpfd < 0) {
perror("open()");
return -1 ;
}


dpev.fd = serverfd;
dpev.events = POLLIN ;

Expand All @@ -116,6 +118,7 @@ int async_queue_init(int serverfd) {
return -1;
}


return dpfd;
}

Expand Down Expand Up @@ -260,11 +263,11 @@ int async_del(int queuefd, int fd, int etype) {

#endif

struct wsgi_request *next_wsgi_req(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
inline struct wsgi_request *next_wsgi_req(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {

uint8_t *ptr = (uint8_t *) wsgi_req ;

ptr += sizeof(struct wsgi_request)+(uwsgi->buffer_size-1) ;
ptr += sizeof(struct wsgi_request) ;

return (struct wsgi_request *) ptr ;
}
Expand Down
26 changes: 23 additions & 3 deletions logging.c
@@ -1,9 +1,15 @@
#include "uwsgi.h"

#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || defined(__OpenBSD__)
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
#include <kvm.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#elif defined(__sun__)
/* Terrible Hack !!! */
#ifndef _LP64
#undef _FILE_OFFSET_BITS
#endif
#include <procfs.h>
#define _FILE_OFFSET_BITS 64
#endif

extern struct uwsgi_server uwsgi;
Expand Down Expand Up @@ -96,6 +102,19 @@ void get_memusage() {
fclose(procfile);
}
uwsgi.workers[uwsgi.mywid].rss_size = uwsgi.workers[uwsgi.mywid].rss_size * uwsgi.page_size;
#elif defined (__sun__)
psinfo_t info;
int procfd ;

procfd = open("/proc/self/psinfo", O_RDONLY);
if (procfd >= 0) {
if ( read(procfd, (char *) &info, sizeof(info)) > 0) {
uwsgi.workers[uwsgi.mywid].rss_size = (uint64_t) info.pr_rssize * 1024 ;
uwsgi.workers[uwsgi.mywid].vsz_size = (uint64_t) info.pr_size * 1024 ;
}
close(procfd);
}

#elif defined( __APPLE__)
/* darwin documentation says that the value are in pages, but they are bytes !!! */
struct task_basic_info t_info;
Expand All @@ -105,13 +124,14 @@ void get_memusage() {
uwsgi.workers[uwsgi.mywid].rss_size = t_info.resident_size;
uwsgi.workers[uwsgi.mywid].vsz_size = t_info.virtual_size;
}
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || defined(__OpenBSD__)
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
kvm_t *kv;
int cnt;

kv = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
if (kv) {
#if defined(__FreeBSD__) || defined(__DragonFly__)

struct kinfo_proc *kproc;
kproc = kvm_getprocs(kv, KERN_PROC_PID, uwsgi.mypid, &cnt);
if (kproc && cnt > 0) {
Expand Down
2 changes: 1 addition & 1 deletion nagios.c
Expand Up @@ -30,7 +30,7 @@ void nagios(struct uwsgi_server *uwsgi) {
exit(2);
}
nagios_poll.events = POLLIN;
if (!uwsgi_parse_response(&nagios_poll, uwsgi->shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) uwsgi->wsgi_req, &uwsgi->wsgi_req->buffer)) {
if (!uwsgi_parse_response(&nagios_poll, uwsgi->shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) uwsgi->wsgi_req, uwsgi->wsgi_req->buffer)) {
fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n");
exit(2);
}
Expand Down
5 changes: 3 additions & 2 deletions protocol.c
Expand Up @@ -292,7 +292,7 @@ int uwsgi_parse_response(struct pollfd *upoll, int timeout, struct uwsgi_header

int uwsgi_parse_vars(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {

char *buffer = &wsgi_req->buffer;
char *buffer = wsgi_req->buffer;

char *ptrbuf, *bufferend;

Expand Down Expand Up @@ -392,6 +392,7 @@ int uwsgi_parse_vars(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req)
// var value
wsgi_req->hvec[wsgi_req->var_cnt].iov_base = ptrbuf;
wsgi_req->hvec[wsgi_req->var_cnt].iov_len = strsize;
//fprintf(stderr,"%.*s = %.*s\n", wsgi_req->hvec[wsgi_req->var_cnt-1].iov_len, wsgi_req->hvec[wsgi_req->var_cnt-1].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len, wsgi_req->hvec[wsgi_req->var_cnt].iov_base);
if (wsgi_req->var_cnt < uwsgi->vec_size - (4 + 1)) {
wsgi_req->var_cnt++;
}
Expand Down Expand Up @@ -454,7 +455,7 @@ int uwsgi_ping_node(int node, struct wsgi_request *wsgi_req) {
}

uwsgi_poll.events = POLLIN;
if (!uwsgi_parse_response(&uwsgi_poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, &wsgi_req->buffer)) {
if (!uwsgi_parse_response(&uwsgi_poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, wsgi_req->buffer)) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion spooler.c
Expand Up @@ -295,7 +295,7 @@ int uwsgi_request_spooler(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_
}

fprintf(stderr, "managing spool request...\n");
i = spool_request(uwsgi, spool_filename, uwsgi->workers[0].requests + 1, &wsgi_req->buffer, wsgi_req->uh.pktsize);
i = spool_request(uwsgi, spool_filename, uwsgi->workers[0].requests + 1, wsgi_req->buffer, wsgi_req->uh.pktsize);
wsgi_req->uh.modifier1 = 255;
wsgi_req->uh.pktsize = 0;
if (i > 0) {
Expand Down
2 changes: 2 additions & 0 deletions ugreen.c
Expand Up @@ -313,6 +313,7 @@ void u_green_init(struct uwsgi_server *uwsgi) {
}
getcontext(uwsgi->ugreen_contexts[i]);
uwsgi->ugreen_contexts[i]->uc_stack.ss_sp = mmap(NULL, u_stack_size + (uwsgi->page_size*2) , PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0) + uwsgi->page_size;

if (!uwsgi->ugreen_contexts[i]->uc_stack.ss_sp) {
perror("mmap()");
exit(1);
Expand All @@ -326,6 +327,7 @@ void u_green_init(struct uwsgi_server *uwsgi) {
perror("mprotect()");
exit(1);
}

uwsgi->ugreen_contexts[i]->uc_stack.ss_size = u_stack_size ;
uwsgi->ugreen_contexts[i]->uc_link = NULL;
makecontext(uwsgi->ugreen_contexts[i], (void (*) (void)) &u_green_request, 3, uwsgi, wsgi_req, i);
Expand Down
3 changes: 2 additions & 1 deletion utils.c
Expand Up @@ -292,6 +292,7 @@ void wsgi_req_setup(struct wsgi_request *wsgi_req, int async_id) {
wsgi_req->async_waiting_fd = -1;
#endif
wsgi_req->hvec = &uwsgi.async_hvec[wsgi_req->async_id];
wsgi_req->buffer = uwsgi.async_buf[wsgi_req->async_id];

}

Expand All @@ -301,7 +302,7 @@ int wsgi_req_recv(struct wsgi_request *wsgi_req) {

if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) gettimeofday(&wsgi_req->start_of_request, NULL);

if (!uwsgi_parse_response(&wsgi_req->poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, &wsgi_req->buffer)) {
if (!uwsgi_parse_response(&wsgi_req->poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, wsgi_req->buffer)) {
return -1;
}

Expand Down
40 changes: 32 additions & 8 deletions uwsgi.c
Expand Up @@ -572,18 +572,33 @@ int main(int argc, char *argv[], char *envp[]) {
}

// allocate more wsgi_req for async mode
uwsgi.wsgi_requests = malloc((sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) ) * uwsgi.async);
uwsgi.wsgi_requests = malloc(sizeof(struct wsgi_request) * uwsgi.async);
if (uwsgi.wsgi_requests == NULL) {
fprintf(stderr, "unable to allocate memory for requests.\n");
exit(1);
}
memset(uwsgi.wsgi_requests, 0, (sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) ) * uwsgi.async);
memset(uwsgi.wsgi_requests, 0, sizeof(struct wsgi_request) * uwsgi.async);

uwsgi.async_buf = malloc( sizeof(char *) * uwsgi.async);
if (!uwsgi.async_buf) {
perror("malloc()");
exit(1);
}

for(i=0;i<uwsgi.async;i++) {
uwsgi.async_buf[i] = malloc(uwsgi.buffer_size);
if (!uwsgi.async_buf[i]) {
perror("malloc()");
exit(1);
}
}


// by default set wsgi_req to the first slot
uwsgi.wsgi_req = uwsgi.wsgi_requests ;

fprintf(stderr, "allocated %llu bytes (%llu KB) for %d request's buffer.\n", (uint64_t) (sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) ) * uwsgi.async,
(uint64_t)( (sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) * uwsgi.async ) / 1024),
fprintf(stderr, "allocated %llu bytes (%llu KB) for %d request's buffer.\n", (uint64_t) (sizeof(struct wsgi_request) * uwsgi.async),
(uint64_t)( (sizeof(struct wsgi_request) * uwsgi.async ) / 1024),
uwsgi.async);

if (uwsgi.synclog) {
Expand All @@ -595,6 +610,7 @@ int main(int argc, char *argv[], char *envp[]) {
}
}


if (uwsgi.pyhome != NULL) {
fprintf(stderr, "Setting PythonHome to %s...\n", uwsgi.pyhome);
#ifdef PYTHREE
Expand Down Expand Up @@ -626,6 +642,8 @@ int main(int argc, char *argv[], char *envp[]) {

Py_Initialize();



#ifdef PYTHREE
mbstowcs(pname, "uwsgi", 6);
pyargv[0] = pname;
Expand All @@ -643,7 +661,13 @@ int main(int argc, char *argv[], char *envp[]) {
wchar_t *wa;
#endif
char *ap;
#ifdef __sun__
// FIX THIS !!!
ap = strtok(uwsgi.pyargv, " ");
while ((ap = strtok(NULL, " ")) != NULL) {
#else
while ((ap = strsep(&uwsgi.pyargv, " \t")) != NULL) {
#endif
if (*ap != '\0') {
#ifdef PYTHREE
wa = (wchar_t *) ( (ap-uwsgi.pyargv) * sizeof(wchar_t) );
Expand Down Expand Up @@ -1204,18 +1228,18 @@ int main(int argc, char *argv[], char *envp[]) {
else if (rlen > 0) {
memset(udp_client_addr, 0, 16);
if (inet_ntop(AF_INET, &udp_client.sin_addr.s_addr, udp_client_addr, 16)) {
if (uwsgi.wsgi_req->buffer == UWSGI_MODIFIER_MULTICAST_ANNOUNCE) {
if (uwsgi.wsgi_req->buffer[0] == UWSGI_MODIFIER_MULTICAST_ANNOUNCE) {
}
#ifdef UWSGI_SNMP
else if (uwsgi.wsgi_req->buffer == 0x30 && uwsgi.snmp) {
else if (uwsgi.wsgi_req->buffer[0] == 0x30 && uwsgi.snmp) {
manage_snmp(uwsgi_poll.fd, (uint8_t *) &uwsgi.wsgi_req->buffer, rlen, &udp_client);
}
#endif
else {
if (udp_callable && udp_callable_args) {
PyTuple_SetItem(udp_callable_args, 0, PyString_FromString(udp_client_addr));
PyTuple_SetItem(udp_callable_args, 1, PyInt_FromLong(ntohs(udp_client.sin_port)));
PyTuple_SetItem(udp_callable_args, 2, PyString_FromStringAndSize(&uwsgi.wsgi_req->buffer, rlen));
PyTuple_SetItem(udp_callable_args, 2, PyString_FromStringAndSize(uwsgi.wsgi_req->buffer, rlen));
PyObject *udp_response = python_call(udp_callable, udp_callable_args);
if (udp_response) {
Py_DECREF(udp_response);
Expand Down Expand Up @@ -1485,7 +1509,7 @@ int main(int argc, char *argv[], char *envp[]) {
goto cycle;
}

wsgi_req_setup(uwsgi.wsgi_req, ( (uint8_t *) uwsgi.wsgi_req - (uint8_t *) uwsgi.wsgi_requests)/(sizeof(struct wsgi_request)+(uwsgi.buffer_size-1))) ;
wsgi_req_setup(uwsgi.wsgi_req, ( (uint8_t *)uwsgi.wsgi_req - (uint8_t *)uwsgi.wsgi_requests)/sizeof(struct wsgi_request) );

if (wsgi_req_accept(uwsgi.serverfd, uwsgi.wsgi_req)) {
continue;
Expand Down
13 changes: 9 additions & 4 deletions uwsgi.h
Expand Up @@ -58,8 +58,9 @@
#undef _POSIX_C_SOURCE
#endif
#ifdef __sun__
#undef _FILE_OFFSET_BITS
#define WAIT_ANY (-1)
#include <sys/filio.h>
#define PRIO_MAX 20
#endif

#define MAX_PYARGV 10
Expand Down Expand Up @@ -160,6 +161,10 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t);
#ifdef __linux__
#include <endian.h>
#elif __sun__
#include <sys/byteorder.h>
#ifdef _BIG_ENDIAN
#define __BIG_ENDIAN__ 1
#endif
#elif __apple__
#include <libkern/OSByteOrder.h>
#else
Expand Down Expand Up @@ -337,8 +342,7 @@ struct wsgi_request {
PyTaskletObject* tasklet;
#endif

// buffer MUST BE THE LAST VAR !!!
char buffer;
char *buffer;
};

struct uwsgi_server {
Expand Down Expand Up @@ -369,6 +373,7 @@ struct uwsgi_server {
#endif

struct iovec *async_hvec;
char **async_buf;

struct rlimit rl;

Expand Down Expand Up @@ -717,7 +722,7 @@ struct wsgi_request *find_first_available_wsgi_req(struct uwsgi_server *);
struct wsgi_request *find_wsgi_req_by_fd(struct uwsgi_server *, int, int);
struct wsgi_request *find_wsgi_req_by_id(struct uwsgi_server *, int);

struct wsgi_request *next_wsgi_req(struct uwsgi_server *, struct wsgi_request *);
inline struct wsgi_request *next_wsgi_req(struct uwsgi_server *, struct wsgi_request *);

int async_add(int, int , int) ;
int async_mod(int, int , int) ;
Expand Down
2 changes: 1 addition & 1 deletion uwsgi_handlers.c
Expand Up @@ -76,7 +76,7 @@ int uwsgi_request_marshal(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_
PyObject *umm = PyDict_GetItemString(uwsgi->embedded_dict,
"message_manager_marshal");
if (umm) {
PyObject *ummo = PyMarshal_ReadObjectFromString(&wsgi_req->buffer,
PyObject *ummo = PyMarshal_ReadObjectFromString(wsgi_req->buffer,
wsgi_req->uh.pktsize);
if (ummo) {
if (!PyTuple_SetItem(uwsgi->embedded_args, 0, ummo)) {
Expand Down
2 changes: 1 addition & 1 deletion uwsgi_pymodule.c
Expand Up @@ -9,7 +9,7 @@ extern struct uwsgi_server uwsgi;
#ifdef __APPLE__
#define UWSGI_LOCK OSSpinLockLock((OSSpinLock *) uwsgi.sharedareamutex);
#define UWSGI_UNLOCK OSSpinLockUnlock((OSSpinLock *) uwsgi.sharedareamutex);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__)
#define UWSGI_LOCK pthread_mutex_lock((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t));
#define UWSGI_UNLOCK pthread_mutex_unlock((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t));
#else
Expand Down
3 changes: 2 additions & 1 deletion uwsgiconfig.py
Expand Up @@ -144,10 +144,11 @@ def parse_vars():
if str(PYLIB_PATH) != '':
ldflags.insert(0,'-L' + PYLIB_PATH)

kvm_list = ['SunOS', 'FreeBSD', 'OpenBSD', 'NetBSD', 'DragonFly']
kvm_list = ['FreeBSD', 'OpenBSD', 'NetBSD', 'DragonFly']

if uwsgi_os == 'SunOS':
ldflags.append('-lsendfile')
ldflags.remove('-rdynamic')

if uwsgi_os in kvm_list:
ldflags.append('-lkvm')
Expand Down

0 comments on commit 47847ee

Please sign in to comment.