Fix "mem" values updated while it should not

Closes #352
This commit is contained in:
Delgan 2024-02-04 16:11:35 +01:00 committed by Daniel Eklöf
parent 195ac5d1cd
commit aeeef4f236
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 16 additions and 7 deletions

View file

@ -39,11 +39,13 @@
* Bar not resizing itself when the screen resolution is changed * Bar not resizing itself when the screen resolution is changed
([#330][330]). ([#330][330]).
* i3/sway: incorrect empty/title state of workspaces ([#343][343]). * i3/sway: incorrect empty/title state of workspaces ([#343][343]).
* mem: state updated on each bar redraw ([#352][352]).
[311]: https://codeberg.org/dnkl/yambar/issues/311 [311]: https://codeberg.org/dnkl/yambar/issues/311
[302]: https://codeberg.org/dnkl/yambar/issues/302 [302]: https://codeberg.org/dnkl/yambar/issues/302
[330]: https://codeberg.org/dnkl/yambar/issues/330 [330]: https://codeberg.org/dnkl/yambar/issues/330
[343]: https://codeberg.org/dnkl/yambar/issues/343 [343]: https://codeberg.org/dnkl/yambar/issues/343
[352]: https://codeberg.org/dnkl/yambar/issues/352
### Security ### Security

View file

@ -24,6 +24,8 @@ struct private
{ {
struct particle *label; struct particle *label;
uint16_t interval; uint16_t interval;
uint64_t mem_free;
uint64_t mem_total;
}; };
static void static void
@ -79,15 +81,12 @@ static struct exposable *
content(struct module *mod) content(struct module *mod)
{ {
const struct private *p = mod->private; const struct private *p = mod->private;
uint64_t mem_free = 0;
uint64_t mem_used = 0;
uint64_t mem_total = 0;
if (!get_mem_stats(&mem_free, &mem_total)) { mtx_lock(&mod->lock);
LOG_ERR("unable to retrieve the memory stats");
}
mem_used = mem_total - mem_free; const uint64_t mem_free = p->mem_free;
const uint64_t mem_total = p->mem_total;
const uint64_t mem_used = mem_total - mem_free;
double percent_used = ((double)mem_used * 100) / (mem_total + 1); double percent_used = ((double)mem_used * 100) / (mem_total + 1);
double percent_free = ((double)mem_free * 100) / (mem_total + 1); double percent_free = ((double)mem_free * 100) / (mem_total + 1);
@ -102,6 +101,7 @@ content(struct module *mod)
struct exposable *exposable = p->label->instantiate(p->label, &tags); struct exposable *exposable = p->label->instantiate(p->label, &tags);
tag_set_destroy(&tags); tag_set_destroy(&tags);
mtx_unlock(&mod->lock);
return exposable; return exposable;
} }
@ -127,6 +127,13 @@ run(struct module *mod)
if (fds[0].revents & POLLIN) if (fds[0].revents & POLLIN)
break; break;
mtx_lock(&mod->lock);
p->mem_free = 0;
p->mem_total = 0;
if (!get_mem_stats(&p->mem_free, &p->mem_total)) {
LOG_ERR("unable to retrieve the memory stats");
}
mtx_unlock(&mod->lock);
bar->refresh(bar); bar->refresh(bar);
} }