summaryrefslogtreecommitdiff
path: root/src/bin/termptysave.c
blob: 0c5ae0153fc8db262d6c6d28639a12809943d82c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "private.h"
#include <Elementary.h>
#include "termpty.h"
#include "termptysave.h"
#include "lz4/lz4.h"
#include <sys/mman.h>

#if defined (__MacOSX__) || (defined (__MACH__) && defined (__APPLE__))
# ifndef MAP_ANONYMOUS
#  define MAP_ANONYMOUS MAP_ANON
# endif
#endif

#define MEM_ALLOC_ALIGN  16
#define MEM_BLOCKS       1024

#define TS_MMAP_SIZE 131072
#define TS_ALLOC_MASK (TS_MMAP_SIZE - 1)

typedef struct _Alloc Alloc;

struct _Alloc
{
   unsigned int size, last, count, allocated;
   short slot;
   unsigned char gen;
   unsigned char __pad;
};

#if 0
static uint64_t _allocated = 0;
#endif
static unsigned char cur_gen = 0;
static Alloc *alloc[MEM_BLOCKS] =  { 0 };

#if 0
static int
roundup_block_size(int sz)
{
   return MEM_ALLOC_ALIGN * ((sz + MEM_ALLOC_ALIGN - 1) / MEM_ALLOC_ALIGN);
}

static Alloc *
_alloc_find(void *mem)
{
   unsigned char *memptr = mem;
   int i;

   for (i = 0; i < MEM_BLOCKS; i++)
     {
        unsigned char *al;

        al = (unsigned char *)alloc[i];
        if (!al) continue;
        if (memptr < al) continue;
        if ((al + TS_MMAP_SIZE) <= memptr) continue;
        return alloc[i];
     }
   return NULL;
}

static void *
_alloc_new(int size, unsigned char gen)
{
   Alloc *al;
   unsigned char *ptr;
   unsigned int newsize, sz;
   int i, firstnull = -1;

   // allocations sized up to nearest size alloc alignment
   newsize = roundup_block_size(size);
   for (i = 0; i < MEM_BLOCKS; i++)
     {
        if (!alloc[i])
          {
             if (firstnull < 0) firstnull = i;
             continue;
          }
        // if generation count matches
        if (alloc[i]->gen == gen)
          {
             // if there is space in the block
             if ((alloc[i]->size - alloc[i]->last) >= newsize)
               {
                  ptr = (unsigned char *)alloc[i];
                  ptr += alloc[i]->last;
                  alloc[i]->last += newsize;
                  alloc[i]->count++;
                  alloc[i]->allocated += newsize;
                  _allocated += newsize;
                  return ptr;
               }
          }
     }
   // out of slots for new blocks - no null blocks
   if (firstnull < 0) {
        ERR("Cannot find new null blocks");
        return NULL;
   }

   // so allocate a new block
   sz = TS_MMAP_SIZE;
   // get mmaped anonymous memory so when freed it goes away from the system
   ptr = (unsigned char*) mmap(NULL, sz, PROT_READ | PROT_WRITE,
              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   if (ptr == MAP_FAILED)
     {
        ERR("Cannot allocate more memory with mmap MAP_ANONYMOUS");
        return NULL;
     }

   // note - we SHOULD memset to 0, but we are assuming mmap anon give 0 pages
   //memset(ptr, 0, newsize);

   al = (Alloc *)ptr;
   al->size = sz;
   al->last = sizeof(Alloc) + newsize;
   al->count = 1;
   al->allocated = newsize;
   al->slot = firstnull;
   al->gen = gen;
   _allocated += newsize;
   alloc[al->slot] = al;
   ptr = (unsigned char *)al;
   ptr += sizeof(Alloc);
   return ptr;
}
#endif

static void *
_ts_new(int size)
{
   /* TODO: RESIZE rewrite that stuff */
   //void *ptr;

   if (!size) return NULL;
   //ptr = _alloc_new(size, cur_gen);

   return calloc(1, size);
}

static void
_ts_free(void *ptr)
{
   free(ptr);
#if 0
   Alloc *al;
   unsigned int sz;
   Termsavecomp *ts = ptr;

   if (!ptr) return;

   if (ts->comp)
     sz = sizeof(Termsavecomp) + ts->w;
   else
     sz = sizeof(Termsave) + ((ts->w - 1) * sizeof(Termcell));
   sz = roundup_block_size(sz);
   _allocated -= sz;

   al = _alloc_find(ptr);
   if (!al)
     {
        ERR("Cannot find %p in alloc blocks", ptr);
        return;
     }
   al->count--;
   al->allocated -= sz;
   if (al->count > 0) return;
   alloc[al->slot] = NULL;
#if defined (__sun) || defined (__sun__)
   munmap((caddr_t)al, al->size);
#else
   munmap(al, al->size);
#endif
#endif
}

static void
_mem_defrag(void)
{
   int i, j = 0;
   Alloc *alloc2[MEM_BLOCKS];

   for (i = 0; i < MEM_BLOCKS; i++)
     {
        if (alloc[i])
          {
//             printf("block %i @ %i [%i/%i] # %i\n",
//                    j, alloc[i]->gen, alloc[i]->last, alloc[i]->size, alloc[i]->count);
             alloc2[j] = alloc[i];
             alloc2[j]->slot = j;
             j++;
          }
     }
   // XXX: quicksort blocks with most space at start
   for (i = 0; i < j; i++) alloc[i] = alloc2[i];
   for (; i < MEM_BLOCKS; i++) alloc[i] = NULL;
}

static void
_mem_gen_next(void)
{
   cur_gen++;
}

static unsigned char
_mem_gen_get(void)
{
   return cur_gen;
}

static int ts_comp = 0;
static int ts_uncomp = 0;
static int ts_freeops = 0;
static int ts_compfreeze = 0;
static int freeze = 0;
static Eina_List *ptys = NULL;
static Ecore_Idler *idler = NULL;
static Ecore_Timer *timer = NULL;

#if 0
static Termsave *
_save_comp(Termsave *ts)
{
   Termsave *ts2;
   Termsavecomp *tsc;

   ERR("save comp");

   // already compacted
   if (ts->comp) return ts;
   // make new allocation for new generation
   ts_compfreeze++;
   if (!ts->z)
     {
        int bytes;
        char *buf;

        buf = alloca(LZ4_compressBound(ts->w * sizeof(Termcell)));
        bytes = LZ4_compress((char *)(&(ts->cells[0])), buf,
                             ts->w * sizeof(Termcell));
        tsc = _ts_new(sizeof(Termsavecomp) + bytes);
        if (!tsc)
          {
             ERR("Big problem. Can't allocate backscroll compress buffer");
             ts2 = ts;
             goto done;
          }
        tsc->comp = 1;
        tsc->z = 1;
        tsc->gen = _mem_gen_get();
        tsc->w = bytes;
        tsc->wout = ts->w;
        memcpy(((char *)tsc) + sizeof(Termsavecomp), buf, bytes);
        ts2 = (Termsave *)tsc;
     }
   else
     {
        tsc = (Termsavecomp *)ts;
        ts2 = _ts_new(sizeof(Termsavecomp) + tsc->w);
        if (!ts2)
          {
             ERR("Big problem. Can't allocate backscroll compress/copy buffer");
             ts2 = ts;
             goto done;
          }
        memcpy(ts2, ts, sizeof(Termsavecomp) + tsc->w);
        ts2->gen = _mem_gen_get();
        ts2->comp = 1;
     }
   termpty_save_free(ts);
done:
   ts_compfreeze--;
   return ts2;
}
#endif

static void
_walk_pty(Termpty *ty)
{
   size_t i;
//   int c0 = 0, c1 = 0;

   if (!ty->back) return;
   for (i = 0; i < ty->backsize; i++)
     {
        Termsavecomp *tsc = (Termsavecomp *)&ty->back[i];

        if (tsc)
          {
#if 0
             ty->back[i] = _save_comp(tsc);
             tsc = (Termsavecomp *)ty->back[i];
             if (tsc->comp) ts_comp++;
             else ts_uncomp++;
//             c0 += tsc->w;
//             c1 += tsc->wout * sizeof(Termcell);
#endif
          }
     }
//   printf("compress ratio: %1.3f\n", (double)c0 / (double)c1);
}

static Eina_Bool
_idler(void *data EINA_UNUSED)
{
   /* TODO: RESIZE : re-enable compression */
   return EINA_FALSE;

   Eina_List *l;
   Termpty *ty;
//   double t0, t;


   _mem_gen_next();

//   t0 = ecore_time_get();
   // start afresh and count comp/uncomp;
   ts_comp = 0;
   ts_uncomp = 0;
   EINA_LIST_FOREACH(ptys, l, ty)
     {
        _walk_pty(ty);
     }
//   t = ecore_time_get();
//   printf("comp/uncomp %i/%i time spent %1.5f\n", ts_comp, ts_uncomp, t - t0);
   _mem_defrag();
   ts_freeops = 0;

   _mem_gen_next();

   idler = NULL;
   return EINA_FALSE;
}

static Eina_Bool
_timer(void *data EINA_UNUSED)
{
   if (!idler) idler = ecore_idler_add(_idler, NULL);
   timer = NULL;
   return EINA_FALSE;
}

static inline void
_check_compressor(Eina_Bool frozen)
{
   /* TODO: RESIZE re-enable compressor */
   return;
   if (freeze) return;
   if (idler) return;
   if ((ts_uncomp > 256) || (ts_freeops > 256))
     {
        if (timer && !frozen) ecore_timer_reset(timer);
        else if (!timer) timer = ecore_timer_add(0.2, _timer, NULL);
     }
}

#if 0
void
termpty_save_freeze(void)
{
   // XXX: suspend compressor - this probably should be in a thread but right
   // now it'll be fine here
   if (!freeze++)
     {
        if (timer) ecore_timer_freeze(timer);
     }
   if (idler)
     {
        ecore_idler_del(idler);
        idler = NULL;
     }
}

void
termpty_save_thaw(void)
{
   // XXX: resume compressor
   freeze--;
   if (freeze <= 0)
     {
        if (timer) ecore_timer_thaw(timer);
        _check_compressor(EINA_TRUE);
     }
}
#endif



void
termpty_save_register(Termpty *ty)
{
   termpty_backlog_lock();
   ptys = eina_list_append(ptys, ty);
   termpty_backlog_unlock();
}

void
termpty_save_unregister(Termpty *ty)
{
   termpty_backlog_lock();
   ptys = eina_list_remove(ptys, ty);
   termpty_backlog_unlock();
}

Termsave *
termpty_save_extract(Termsave *ts)
{
   if (!ts) return NULL;
   if (ts->z)
     {
        Termsavecomp *tsc = (Termsavecomp *)ts;
        Termsave *ts2;
        char *buf;
        int bytes;

        ts2 = _ts_new(sizeof(Termsave) + ((tsc->wout - 1) * sizeof(Termcell)));
        if (!ts2) return NULL;
        ts2->gen = _mem_gen_get();
        ts2->w = tsc->wout;
        buf = ((char *)tsc) + sizeof(Termsavecomp);
        bytes = LZ4_uncompress(buf, (char *)(&(ts2->cells[0])),
                               tsc->wout * sizeof(Termcell));
        if (bytes < 0)
          {
             memset(&(ts2->cells[0]), 0, tsc->wout * sizeof(Termcell));
//             ERR("Decompress problem in row at byte %i", -bytes);
          }
        if (ts->comp) ts_comp--;
        else ts_uncomp--;
        ts_uncomp++;
        ts_freeops++;
        ts_compfreeze++;
        _ts_free(ts);
        ts_compfreeze--;
        _check_compressor(EINA_FALSE);
        return ts2;
     }
   _check_compressor(EINA_FALSE);
   return ts;
}

Termsave *
termpty_save_new(Termsave *ts, int w)
{
   termpty_save_free(ts);

   Termcell *cells = calloc(1, w * sizeof(Termcell));
   if (!cells ) return NULL;
   ts->cells = cells;
   ts->gen = _mem_gen_get();
   ts->w = w;
   if (!ts_compfreeze) ts_uncomp++;
   _check_compressor(EINA_FALSE);
   return ts;
}

Termsave *
termpty_save_expand(Termsave *ts, Termcell *cells, size_t delta)
{
   Termcell *newcells;

   newcells = realloc(ts->cells, (ts->w + delta) * sizeof(Termcell));
   if (!newcells)
     return NULL;
   newcells[ts->w - 1].att.autowrapped = 0;
   memcpy(&newcells[ts->w], cells, delta * sizeof(Termcell));
   ts->w += delta;
   ts->cells = newcells;
   return ts;
}

void
termpty_save_free(Termsave *ts)
{
   if (!ts) return;
   if (!ts_compfreeze)
     {
        if (ts->comp) ts_comp--;
        else ts_uncomp--;
        ts_freeops++;
     }
   _ts_free(ts->cells);
   ts->cells = NULL;
   ts->w = 0;
   _check_compressor(EINA_FALSE);
}

void
termpty_backlog_lock(void)
{
}

void
termpty_backlog_unlock(void)
{
}