Browse Source

Lower thread stack to 128k

Georgi Chorbadzhiyski 12 years ago
parent
commit
40a64d59b6
5 changed files with 26 additions and 4 deletions
  1. 1
    1
      camd.c
  2. 8
    0
      data.c
  3. 5
    0
      data.h
  4. 1
    1
      notify.c
  5. 11
    2
      tsdecrypt.c

+ 1
- 1
camd.c View File

@@ -296,7 +296,7 @@ void camd_start(struct ts *ts) {
296 296
 		c->req_queue = queue_new();
297 297
 		c->ecm_queue = queue_new();
298 298
 		c->emm_queue = queue_new();
299
-		pthread_create(&c->thread, NULL , &camd_thread, ts);
299
+		pthread_create(&c->thread, &ts->thread_attr , &camd_thread, ts);
300 300
 	}
301 301
 }
302 302
 

+ 8
- 0
data.c View File

@@ -105,6 +105,12 @@ void data_init(struct ts *ts) {
105 105
 	ts->write_buf   = cbuf_init((7 * dvbcsa_bs_batch_size() * 188) *  8, "write");  // ~324Kb
106 106
 
107 107
 	ts->input_buffer= list_new("input");
108
+
109
+	pthread_attr_init(&ts->thread_attr);
110
+	size_t stack_size;
111
+	pthread_attr_getstacksize(&ts->thread_attr, &stack_size);
112
+	if (stack_size > THREAD_STACK_SIZE)
113
+		pthread_attr_setstacksize(&ts->thread_attr, THREAD_STACK_SIZE);
108 114
 }
109 115
 
110 116
 void data_free(struct ts *ts) {
@@ -143,4 +149,6 @@ void data_free(struct ts *ts) {
143 149
 	// and in order to avoid leaking 43 bytes of memory on exit which makes valgrind
144 150
 	// unhappy it is a good idea to free the memory (ONCE!).
145 151
 	FREE(ts->camd.newcamd.crypt_passwd);
152
+
153
+	pthread_attr_destroy(&ts->thread_attr);
146 154
 }

+ 5
- 0
data.h View File

@@ -42,6 +42,9 @@
42 42
 #define ECM_QUEUE_HARD_LIMIT 10
43 43
 #define ECM_QUEUE_SOFT_LIMIT 3
44 44
 
45
+// 64k should be enough for everybody
46
+#define THREAD_STACK_SIZE (64 * 1024)
47
+
45 48
 struct notify {
46 49
 	pthread_t	thread;				/* Thread handle */
47 50
 	QUEUE		*notifications;		/* Notification queue */
@@ -273,6 +276,8 @@ struct ts {
273 276
 
274 277
 	int					threaded;
275 278
 
279
+	pthread_attr_t		thread_attr;
280
+
276 281
 	int					decode_stop;
277 282
 	pthread_t			decode_thread;
278 283
 	CBUF				*decode_buf;

+ 1
- 1
notify.c View File

@@ -128,7 +128,7 @@ struct notify *notify_alloc(struct ts *ts) {
128 128
 	}
129 129
 	strncpy(n->program, ts->notify_program, sizeof(n->program) - 1);
130 130
 	n->program[sizeof(n->program) - 1] = '\0';
131
-	pthread_create(&n->thread, NULL , &notify_thread, n);
131
+	pthread_create(&n->thread, &ts->thread_attr , &notify_thread, n);
132 132
 	return n;
133 133
 }
134 134
 

+ 11
- 2
tsdecrypt.c View File

@@ -25,6 +25,7 @@
25 25
 #include <fcntl.h>
26 26
 #include <errno.h>
27 27
 #include <syslog.h>
28
+#include <sys/resource.h>
28 29
 
29 30
 #include <dvbcsa/dvbcsa.h>
30 31
 #include <openssl/rand.h>
@@ -780,6 +781,14 @@ int main(int argc, char **argv) {
780 781
 	int ntimeouts = 0;
781 782
 	time_t timeout_start = time(NULL);
782 783
 	int rtp_hdr_pos = 0, num_packets = 0;
784
+	struct rlimit rl;
785
+
786
+	if (getrlimit(RLIMIT_STACK, &rl) == 0) {
787
+		if (rl.rlim_cur > THREAD_STACK_SIZE) {
788
+			rl.rlim_cur = THREAD_STACK_SIZE;
789
+			setrlimit(RLIMIT_STACK, &rl);
790
+		}
791
+	}
783 792
 
784 793
 	memset(rtp_hdr[0], 0, RTP_HDR_SZ);
785 794
 	memset(rtp_hdr[1], 0, RTP_HDR_SZ);
@@ -820,8 +829,8 @@ int main(int argc, char **argv) {
820 829
 	signal(SIGTERM, signal_quit);
821 830
 
822 831
 	if (ts.threaded) {
823
-		pthread_create(&ts.decode_thread, NULL, &decode_thread, &ts);
824
-		pthread_create(&ts.write_thread, NULL , &write_thread , &ts);
832
+		pthread_create(&ts.decode_thread, &ts.thread_attr, &decode_thread, &ts);
833
+		pthread_create(&ts.write_thread , &ts.thread_attr, &write_thread , &ts);
825 834
 	}
826 835
 
827 836
 	ts.emm_last_report = time(NULL) + FIRST_REPORT_SEC;

Loading…
Cancel
Save