Line data Source code
1 : /* 2 : * Copyright (C) 2016 Andrea Mazzoleni 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "portable.h" 19 : 20 : #include "bw.h" 21 : 22 215 : void bw_init(struct snapraid_bw* bw, uint64_t limit) 23 : { 24 215 : bw->limit = limit; 25 215 : bw->total = 0; 26 215 : bw->start = tick_ms(); 27 215 : } 28 : 29 9469208 : void bw_limit(struct snapraid_bw* bw, uint64_t bytes) 30 : { 31 9469208 : if (!bw || bw->limit == 0) 32 9469152 : return; 33 : 34 56 : uint64_t elapsed = tick_ms() - bw->start; 35 : uint64_t done; 36 : uint64_t eta; 37 : 38 56 : done = __atomic_fetch_add(&bw->total, bytes, __ATOMIC_SEQ_CST); 39 : 40 56 : eta = done * 1000 / bw->limit; 41 : 42 56 : if (eta > elapsed) { 43 49 : eta -= elapsed; 44 49 : usleep(eta * 1000); 45 : } 46 : } 47 :