Skip to content

Commit

Permalink
add stat for ASUs and send them #1582
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Jun 22, 2021
1 parent e909e6d commit 46f5537
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rearrangements of Notcurses.
`ncpile_render_to_file()`. Rewrote `notcurses_render_to_buffer()` and
`notcurses_render_to_file()` as trivial wrappers around these functions,
and deprecated the latter. They will be removed in ABI3.
* Added support for application-synchronized updates, and a new stat.

* 2.3.4 (2021-06-12)
* Added the flag `NCVISUAL_OPTION_NOINTERPOLATE` to use non-interpolative
Expand Down
1 change: 1 addition & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,7 @@ typedef struct ncstats {
uint64_t sprixelemissions; // sprixel draw count
uint64_t sprixelelisions; // sprixel elision count
uint64_t sprixelbytes; // sprixel bytes emitted
uint64_t appsync_updates; // application-synchronized updates
// current state -- these can decrease
uint64_t fbbytes; // total bytes devoted to all active framebuffers
Expand Down
1 change: 1 addition & 0 deletions doc/man/man3/notcurses_stats.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct ncstats {
uint64_t sprixelemissions; // sprixel draw count
uint64_t sprixelelisions; // sprixel elision count
uint64_t sprixelbytes; // sprixel bytes emitted
uint64_t appsync_updates; // application-synchronized updates

// current state -- these can decrease
uint64_t fbbytes; // bytes devoted to framebuffers
Expand Down
1 change: 1 addition & 0 deletions include/notcurses/notcurses.h
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,7 @@ typedef struct ncstats {
uint64_t defaultelisions; // default color was emitted
uint64_t defaultemissions; // default color was elided
uint64_t refreshes; // refresh requests (non-optimized redraw)
uint64_t appsync_updates; // how many application-synchronized updates?

// current state -- these can decrease
uint64_t fbbytes; // total bytes devoted to all active framebuffers
Expand Down
2 changes: 1 addition & 1 deletion src/lib/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ stash_string(query_state* inits){
// ought be fed to the machine, and -1 on an invalid state transition.
static int
pump_control_read(query_state* inits, unsigned char c){
fprintf(stderr, "state: %2d char: %1c %3d %02x\n", inits->state, isprint(c) ? c : ' ', c, c);
//fprintf(stderr, "state: %2d char: %1c %3d %02x\n", inits->state, isprint(c) ? c : ' ', c, c);
if(c == NCKEY_ESC){
inits->state = STATE_ESC;
return 0;
Expand Down
38 changes: 34 additions & 4 deletions src/lib/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,12 @@ rasterize_core(notcurses* nc, const ncpile* p, FILE* out, unsigned phase){
return 0;
}

// 'asu' on input is non-0 if application-synchronized updates are permitted
// (they are not, for instance, when rendering to a non-tty). on output,
// assuming success, it is non-0 if application-synchronized updates are
// desired; in this case, an ASU footer is present at the end of the buffer.
static int
notcurses_rasterize_inner(notcurses* nc, ncpile* p, FILE* out){
notcurses_rasterize_inner(notcurses* nc, ncpile* p, FILE* out, unsigned* asu){
fseeko(out, 0, SEEK_SET);
// we only need to emit a coordinate if it was damaged. the damagemap is a
// bit per coordinate, one per struct crender.
Expand Down Expand Up @@ -1130,19 +1134,44 @@ notcurses_rasterize_inner(notcurses* nc, ncpile* p, FILE* out){
if(fflush(out)){
return -1;
}
#define MIN_ASU_SIZE 4096 // FIXME
if(*asu){
if(nc->rstate.mstrsize >= MIN_ASU_SIZE){
const char* endasu = get_escape(&nc->tcache, ESCAPE_ESU);
if(endasu){
if(fprintf(out, endasu) < 0 || fflush(out)){
return -1;
}
}else{
*asu = 0;
}
}else{
*asu = 0;
}
}
#undef MIN_ASU_SIZE
return nc->rstate.mstrsize;
}

// rasterize the rendered frame, and blockingly write it out to the terminal.
static int
raster_and_write(notcurses* nc, ncpile* p, FILE* out){
if(notcurses_rasterize_inner(nc, p, out) < 0){
// will we be using application-synchronized updates? if this comes back as
// non-zero, we are, and must emit the header. no ASU without a tty.
unsigned useasu = nc->ttyfd >= 0 ? true : false;
if(notcurses_rasterize_inner(nc, p, out, &useasu) < 0){
return -1;
}
int ret = 0;
//fflush(nc->ttyfp);
sigset_t oldmask;
block_signals(&oldmask);
if(useasu){
const char* basu = get_escape(&nc->tcache, ESCAPE_BSU);
if(tty_emit(basu, nc->ttyfd)){
ret = -1;
}
++nc->stats.appsync_updates;
}
if(blocking_write(fileno(nc->ttyfp), nc->rstate.mstream, nc->rstate.mstrsize)){
ret = -1;
}
Expand Down Expand Up @@ -1384,7 +1413,8 @@ int ncpile_render_to_buffer(ncplane* p, char** buf, size_t* buflen){
return -1;
}
notcurses* nc = ncplane_notcurses(p);
int bytes = notcurses_rasterize_inner(nc, ncplane_pile(p), nc->rstate.mstreamfp);
unsigned useacu = false; // no ACU to file
int bytes = notcurses_rasterize_inner(nc, ncplane_pile(p), nc->rstate.mstreamfp, &useacu);
pthread_mutex_lock(&nc->statlock);
update_render_bytes(&nc->stats, bytes);
pthread_mutex_unlock(&nc->statlock);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void notcurses_stats_reset(notcurses* nc, ncstats* stats){
stash->sprixelemissions += nc->stats.sprixelemissions;
stash->sprixelelisions += nc->stats.sprixelelisions;
stash->sprixelbytes += nc->stats.sprixelbytes;
stash->appsync_updates += nc->stats.appsync_updates;

stash->fbbytes = nc->stats.fbbytes;
stash->planes = nc->stats.planes;
Expand Down Expand Up @@ -214,10 +215,10 @@ void summarize_stats(notcurses* nc){
(stats->bgelisions * 100.0) / (stats->bgemissions + stats->bgelisions));
char totalbuf[BPREFIXSTRLEN + 1];
qprefix(stats->sprixelbytes, 1, totalbuf, 1);
fprintf(stderr, "Sprixel emits:elides: %ju:%ju (%.2f%%) %sB\n",
fprintf(stderr, "Sprixel emits:elides: %ju:%ju (%.2f%%) %sB ASUs: %ju\n",
stats->sprixelemissions, stats->sprixelelisions,
(stats->sprixelemissions + stats->sprixelelisions) == 0 ? 0 :
(stats->sprixelelisions * 100.0) / (stats->sprixelemissions + stats->sprixelelisions),
totalbuf);
totalbuf, stats->appsync_updates);
}
}

0 comments on commit 46f5537

Please sign in to comment.