Changeset b5ba8f6 in mainline for kernel/generic/src/mm/frame.c
- Timestamp:
- 2013-09-13T13:11:53Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1eaa3cf
- Parents:
- 95027b5 (diff), 1c5f6f8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/mm/frame.c
r95027b5 rb5ba8f6 60 60 #include <config.h> 61 61 #include <str.h> 62 63 #define BITMAP_BLOCK_SIZE 12864 62 65 63 zones_t zones; … … 236 234 * the bitmap if the last argument is NULL. 237 235 */ 236 238 237 return ((zone->flags & ZONE_AVAILABLE) && 239 238 bitmap_allocate_range(&zone->bitmap, count, zone->base, 240 constraint, NULL)); 239 FRAME_LOWPRIO, constraint, NULL)); 240 } 241 242 /** Find a zone that can allocate specified number of frames 243 * 244 * This function searches among all zones. Assume interrupts are 245 * disabled and zones lock is locked. 246 * 247 * @param count Number of free frames we are trying to find. 248 * @param flags Required flags of the zone. 249 * @param constraint Indication of bits that cannot be set in the 250 * physical frame number of the first allocated frame. 251 * @param hint Preferred zone. 252 * 253 * @return Zone that can allocate specified number of frames. 254 * @return -1 if no zone can satisfy the request. 255 * 256 */ 257 NO_TRACE static size_t find_free_zone_all(size_t count, zone_flags_t flags, 258 pfn_t constraint, size_t hint) 259 { 260 for (size_t pos = 0; pos < zones.count; pos++) { 261 size_t i = (pos + hint) % zones.count; 262 263 /* Check whether the zone meets the search criteria. */ 264 if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags)) 265 continue; 266 267 /* Check if the zone can satisfy the allocation request. */ 268 if (zone_can_alloc(&zones.info[i], count, constraint)) 269 return i; 270 } 271 272 return (size_t) -1; 273 } 274 275 /** Check if frame range priority memory 276 * 277 * @param pfn Starting frame. 278 * @param count Number of frames. 279 * 280 * @return True if the range contains only priority memory. 281 * 282 */ 283 NO_TRACE static bool is_high_priority(pfn_t base, size_t count) 284 { 285 return (base + count <= FRAME_LOWPRIO); 286 } 287 288 /** Find a zone that can allocate specified number of frames 289 * 290 * This function ignores zones that contain only high-priority 291 * memory. Assume interrupts are disabled and zones lock is locked. 292 * 293 * @param count Number of free frames we are trying to find. 294 * @param flags Required flags of the zone. 295 * @param constraint Indication of bits that cannot be set in the 296 * physical frame number of the first allocated frame. 297 * @param hint Preferred zone. 298 * 299 * @return Zone that can allocate specified number of frames. 300 * @return -1 if no low-priority zone can satisfy the request. 301 * 302 */ 303 NO_TRACE static size_t find_free_zone_lowprio(size_t count, zone_flags_t flags, 304 pfn_t constraint, size_t hint) 305 { 306 for (size_t pos = 0; pos < zones.count; pos++) { 307 size_t i = (pos + hint) % zones.count; 308 309 /* Skip zones containing only high-priority memory. */ 310 if (is_high_priority(zones.info[i].base, zones.info[i].count)) 311 continue; 312 313 /* Check whether the zone meets the search criteria. */ 314 if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags)) 315 continue; 316 317 /* Check if the zone can satisfy the allocation request. */ 318 if (zone_can_alloc(&zones.info[i], count, constraint)) 319 return i; 320 } 321 322 return (size_t) -1; 241 323 } 242 324 … … 250 332 * @param constraint Indication of bits that cannot be set in the 251 333 * physical frame number of the first allocated frame. 252 * @param hind Preferred zone. 334 * @param hint Preferred zone. 335 * 336 * @return Zone that can allocate specified number of frames. 337 * @return -1 if no zone can satisfy the request. 253 338 * 254 339 */ … … 259 344 hint = 0; 260 345 261 size_t i = hint; 262 do { 263 /* 264 * Check whether the zone meets the search criteria. 265 */ 266 if (ZONE_FLAGS_MATCH(zones.info[i].flags, flags)) { 267 /* 268 * Check if the zone can satisfy the allocation request. 269 */ 270 if (zone_can_alloc(&zones.info[i], count, constraint)) 271 return i; 272 } 273 274 i++; 275 if (i >= zones.count) 276 i = 0; 277 278 } while (i != hint); 279 280 return (size_t) -1; 346 /* 347 * Prefer zones with low-priority memory over 348 * zones with high-priority memory. 349 */ 350 351 size_t znum = find_free_zone_lowprio(count, flags, constraint, hint); 352 if (znum != (size_t) -1) 353 return znum; 354 355 /* Take all zones into account */ 356 return find_free_zone_all(count, flags, constraint, hint); 281 357 } 282 358 … … 314 390 size_t index; 315 391 int avail = bitmap_allocate_range(&zone->bitmap, count, zone->base, 316 constraint, &index);392 FRAME_LOWPRIO, constraint, &index); 317 393 318 394 ASSERT(avail); … … 367 443 NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t index) 368 444 { 369 if (!(zone->flags & ZONE_AVAILABLE)) 370 return; 445 ASSERT(zone->flags & ZONE_AVAILABLE); 371 446 372 447 frame_t *frame = zone_get_frame(zone, index); … … 410 485 411 486 bitmap_initialize(&zones.info[z1].bitmap, zones.info[z1].count, 412 BITMAP_BLOCK_SIZE, confdata + 413 (sizeof(frame_t) * zones.info[z1].count)); 487 confdata + (sizeof(frame_t) * zones.info[z1].count)); 414 488 bitmap_clear_range(&zones.info[z1].bitmap, 0, zones.info[z1].count); 415 489 … … 578 652 */ 579 653 580 bitmap_initialize(&zone->bitmap, count, BITMAP_BLOCK_SIZE,581 confdata +(sizeof(frame_t) * count));654 bitmap_initialize(&zone->bitmap, count, confdata + 655 (sizeof(frame_t) * count)); 582 656 bitmap_clear_range(&zone->bitmap, 0, count); 583 657 … … 591 665 frame_initialize(&zone->frames[i]); 592 666 } else { 593 bitmap_initialize(&zone->bitmap, 0, 0,NULL);667 bitmap_initialize(&zone->bitmap, 0, NULL); 594 668 zone->frames = NULL; 595 669 } … … 605 679 size_t zone_conf_size(size_t count) 606 680 { 607 return (count * sizeof(frame_t) + 608 bitmap_size(count, BITMAP_BLOCK_SIZE)); 681 return (count * sizeof(frame_t) + bitmap_size(count)); 609 682 } 610 683 … … 887 960 } 888 961 889 uintptr_t frame_alloc_noreserve(size_t count, frame_flags_t flags,890 uintptr_t constraint)891 {892 return frame_alloc_generic(count, flags | FRAME_NO_RESERVE, constraint,893 NULL);894 }895 896 962 /** Free frames of physical memory. 897 963 * … … 1141 1207 /* 1142 1208 * Because printing may require allocation of memory, we may not hold 1143 * the frame allocator locks when printing zone statistics. 1209 * the frame allocator locks when printing zone statistics. Therefore, 1144 1210 * we simply gather the statistics under the protection of the locks and 1145 1211 * print the statistics when the locks have been released. … … 1150 1216 */ 1151 1217 1218 size_t free_lowmem = 0; 1219 size_t free_highmem = 0; 1220 size_t free_highprio = 0; 1221 1152 1222 for (size_t i = 0;; i++) { 1153 1223 irq_spinlock_lock(&zones.lock, true); … … 1158 1228 } 1159 1229 1160 uintptr_t base = PFN2ADDR(zones.info[i].base); 1230 pfn_t fbase = zones.info[i].base; 1231 uintptr_t base = PFN2ADDR(fbase); 1161 1232 size_t count = zones.info[i].count; 1162 1233 zone_flags_t flags = zones.info[i].flags; … … 1164 1235 size_t busy_count = zones.info[i].busy_count; 1165 1236 1237 bool available = ((flags & ZONE_AVAILABLE) != 0); 1238 bool lowmem = ((flags & ZONE_LOWMEM) != 0); 1239 bool highmem = ((flags & ZONE_HIGHMEM) != 0); 1240 bool highprio = is_high_priority(fbase, count); 1241 1242 if (available) { 1243 if (lowmem) 1244 free_lowmem += free_count; 1245 1246 if (highmem) 1247 free_highmem += free_count; 1248 1249 if (highprio) { 1250 free_highprio += free_count; 1251 } else { 1252 /* 1253 * Walk all frames of the zone and examine 1254 * all high priority memory to get accurate 1255 * statistics. 1256 */ 1257 1258 for (size_t index = 0; index < count; index++) { 1259 if (is_high_priority(fbase + index, 0)) { 1260 if (!bitmap_get(&zones.info[i].bitmap, index)) 1261 free_highprio++; 1262 } else 1263 break; 1264 } 1265 } 1266 } 1267 1166 1268 irq_spinlock_unlock(&zones.lock, true); 1167 1168 bool available = ((flags & ZONE_AVAILABLE) != 0);1169 1269 1170 1270 printf("%-4zu", i); … … 1191 1291 printf("\n"); 1192 1292 } 1293 1294 printf("\n"); 1295 1296 uint64_t size; 1297 const char *size_suffix; 1298 1299 bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix, 1300 false); 1301 printf("Available low memory: %zu frames (%" PRIu64 " %s)\n", 1302 free_lowmem, size, size_suffix); 1303 1304 bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix, 1305 false); 1306 printf("Available high memory: %zu frames (%" PRIu64 " %s)\n", 1307 free_highmem, size, size_suffix); 1308 1309 bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix, 1310 false); 1311 printf("Available high priority: %zu frames (%" PRIu64 " %s)\n", 1312 free_highprio, size, size_suffix); 1193 1313 } 1194 1314 … … 1216 1336 } 1217 1337 1218 uintptr_t base = PFN2ADDR(zones.info[znum].base); 1338 size_t free_lowmem = 0; 1339 size_t free_highmem = 0; 1340 size_t free_highprio = 0; 1341 1342 pfn_t fbase = zones.info[znum].base; 1343 uintptr_t base = PFN2ADDR(fbase); 1219 1344 zone_flags_t flags = zones.info[znum].flags; 1220 1345 size_t count = zones.info[znum].count; … … 1222 1347 size_t busy_count = zones.info[znum].busy_count; 1223 1348 1349 bool available = ((flags & ZONE_AVAILABLE) != 0); 1350 bool lowmem = ((flags & ZONE_LOWMEM) != 0); 1351 bool highmem = ((flags & ZONE_HIGHMEM) != 0); 1352 bool highprio = is_high_priority(fbase, count); 1353 1354 if (available) { 1355 if (lowmem) 1356 free_lowmem = free_count; 1357 1358 if (highmem) 1359 free_highmem = free_count; 1360 1361 if (highprio) { 1362 free_highprio = free_count; 1363 } else { 1364 /* 1365 * Walk all frames of the zone and examine 1366 * all high priority memory to get accurate 1367 * statistics. 1368 */ 1369 1370 for (size_t index = 0; index < count; index++) { 1371 if (is_high_priority(fbase + index, 0)) { 1372 if (!bitmap_get(&zones.info[znum].bitmap, index)) 1373 free_highprio++; 1374 } else 1375 break; 1376 } 1377 } 1378 } 1379 1224 1380 irq_spinlock_unlock(&zones.lock, true); 1225 1226 bool available = ((flags & ZONE_AVAILABLE) != 0);1227 1381 1228 1382 uint64_t size; 1229 1383 const char *size_suffix; 1384 1230 1385 bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false); 1231 1386 1232 printf("Zone number: %zu\n", znum);1233 printf("Zone base address: %p\n", (void *) base);1234 printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,1387 printf("Zone number: %zu\n", znum); 1388 printf("Zone base address: %p\n", (void *) base); 1389 printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count, 1235 1390 size, size_suffix); 1236 printf("Zone flags: %c%c%c%c%c\n",1391 printf("Zone flags: %c%c%c%c%c\n", 1237 1392 available ? 'A' : '-', 1238 1393 (flags & ZONE_RESERVED) ? 'R' : '-', … … 1244 1399 bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix, 1245 1400 false); 1246 printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",1401 printf("Allocated space: %zu frames (%" PRIu64 " %s)\n", 1247 1402 busy_count, size, size_suffix); 1403 1248 1404 bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix, 1249 1405 false); 1250 printf("Available space: %zu frames (%" PRIu64 " %s)\n",1406 printf("Available space: %zu frames (%" PRIu64 " %s)\n", 1251 1407 free_count, size, size_suffix); 1408 1409 bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix, 1410 false); 1411 printf("Available low memory: %zu frames (%" PRIu64 " %s)\n", 1412 free_lowmem, size, size_suffix); 1413 1414 bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix, 1415 false); 1416 printf("Available high memory: %zu frames (%" PRIu64 " %s)\n", 1417 free_highmem, size, size_suffix); 1418 1419 bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix, 1420 false); 1421 printf("Available high priority: %zu frames (%" PRIu64 " %s)\n", 1422 free_highprio, size, size_suffix); 1252 1423 } 1253 1424 }
Note:
See TracChangeset
for help on using the changeset viewer.