~ubuntu-branches/ubuntu/precise/ceph/precise

« back to all changes in this revision

Viewing changes to src/crush/CrushWrapper.h

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum, Clint Byrum, Micah Gersten
  • Date: 2011-02-12 22:50:26 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110212225026-yyyw4tk0msgql3ul
Tags: 0.24.2-0ubuntu1
[ Clint Byrum <clint@ubuntu.com> ]
* New upstream release. (LP: #658670, LP: #684011)
* debian/patches/fix-mkcephfs.patch: dropped (applied upstream)
* Removed .la files from libceph1-dev, libcrush1-dev and 
  librados1-dev (per Debian policy v3.9.1 10.2).
* debian/control: adding pkg-config as a build dependency
* debian/control: depend on libcrypto++-dev instead of libssl-dev
* debian/watch: added watch file

[ Micah Gersten <micahg@ubuntu.com> ]
* debian/control: add Homepage

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
    build_rmap(rule_name_map, rule_name_rmap);
62
62
    have_rmaps = true;
63
63
  }
64
 
  void build_rmap(map<int, string> &f, std::map<string, int> &r) {
 
64
  void build_rmap(const map<int, string> &f, std::map<string, int> &r) {
65
65
    r.clear();
66
 
    for (std::map<int, string>::iterator p = f.begin(); p != f.end(); p++)
 
66
    for (std::map<int, string>::const_iterator p = f.begin(); p != f.end(); ++p)
67
67
      r[p->second] = p->first;
68
68
  }
69
69
 
256
256
  /** buckets **/
257
257
private:
258
258
  crush_bucket *get_bucket(int id) {
259
 
    if (!crush) return (crush_bucket *)(-ENOENT);
260
 
    int pos = -1 - id;
261
 
    if (pos >= crush->max_buckets) return 0;
262
 
    return crush->buckets[pos];
 
259
    if (!crush)
 
260
      return (crush_bucket *)(-EINVAL);
 
261
    unsigned int pos = (unsigned int)(-1 - id);
 
262
    unsigned int max_buckets = crush->max_buckets;
 
263
    if (pos >= max_buckets)
 
264
      return (crush_bucket *)(-ENOENT);
 
265
    crush_bucket *ret = crush->buckets[pos];
 
266
    if (ret == NULL)
 
267
      return (crush_bucket *)(-ENOENT);
 
268
    return ret;
263
269
  }
264
270
 
265
271
public:
273
279
  }
274
280
  bool bucket_exists(int id) {
275
281
    crush_bucket *b = get_bucket(id);
276
 
    if (b == 0 || IS_ERR(b)) return false;
 
282
    if (IS_ERR(b))
 
283
      return false;
277
284
    return true;
278
285
  }
279
286
  int get_bucket_weight(int id) {
304
311
  int get_bucket_item(int id, int pos) {
305
312
    crush_bucket *b = get_bucket(id);
306
313
    if (IS_ERR(b)) return PTR_ERR(b);
 
314
    if ((__u32)pos >= b->size)
 
315
      return PTR_ERR(b);
307
316
    return b->items[pos];
308
317
  }
309
318
  int get_bucket_item_weight(int id, int pos) {
429
438
    ::encode(rule_name_map, bl);
430
439
  }
431
440
 
432
 
  void decode(bufferlist::iterator &blp) {
 
441
  void decode(bufferlist::iterator &blp)
 
442
  {
433
443
    create();
434
444
 
435
445
    __u32 magic;
436
446
    ::decode(magic, blp);
437
 
    assert(magic == CRUSH_MAGIC);
 
447
    if (magic != CRUSH_MAGIC)
 
448
      throw buffer::malformed_input("bad magic number");
438
449
 
439
450
    ::decode(crush->max_buckets, blp);
440
451
    ::decode(crush->max_rules, blp);
441
452
    ::decode(crush->max_devices, blp);
442
453
 
443
 
    // buckets
444
 
    crush->buckets = (crush_bucket**)malloc(sizeof(crush_bucket*)*crush->max_buckets);
445
 
    for (int i=0; i<crush->max_buckets; i++) {
446
 
      __u32 alg;
447
 
      ::decode(alg, blp);
448
 
      if (!alg) {
449
 
        crush->buckets[i] = 0;
450
 
        continue;
451
 
      }
452
 
 
453
 
      int size = 0;
454
 
      switch (alg) {
 
454
    try {
 
455
      // buckets
 
456
      crush->buckets = (crush_bucket**)calloc(1, crush->max_buckets * sizeof(crush_bucket*));
 
457
      for (int i=0; i<crush->max_buckets; i++) {
 
458
        decode_crush_bucket(&crush->buckets[i], blp);
 
459
      }
 
460
 
 
461
      // rules
 
462
      crush->rules = (crush_rule**)calloc(1, crush->max_rules * sizeof(crush_rule*));
 
463
      for (unsigned i = 0; i < crush->max_rules; ++i) {
 
464
        __u32 yes;
 
465
        ::decode(yes, blp);
 
466
        if (!yes) {
 
467
          crush->rules[i] = NULL;
 
468
          continue;
 
469
        }
 
470
 
 
471
        __u32 len;
 
472
        ::decode(len, blp);
 
473
        crush->rules[i] = (crush_rule*)calloc(1, crush_rule_size(len));
 
474
        crush->rules[i]->len = len;
 
475
        ::decode(crush->rules[i]->mask, blp);
 
476
        for (unsigned j=0; j<crush->rules[i]->len; j++)
 
477
          ::decode(crush->rules[i]->steps[j], blp);
 
478
      }
 
479
 
 
480
      // name info
 
481
      ::decode(type_map, blp);
 
482
      ::decode(name_map, blp);
 
483
      ::decode(rule_name_map, blp);
 
484
      build_rmaps();
 
485
 
 
486
      finalize();
 
487
    }
 
488
    catch (...) {
 
489
      crush_destroy(crush);
 
490
      throw;
 
491
    }
 
492
  }
 
493
 
 
494
  void decode_crush_bucket(crush_bucket** bptr, bufferlist::iterator &blp)
 
495
  {
 
496
    __u32 alg;
 
497
    ::decode(alg, blp);
 
498
    if (!alg) {
 
499
      *bptr = NULL;
 
500
      return;
 
501
    }
 
502
 
 
503
    int size = 0;
 
504
    switch (alg) {
455
505
      case CRUSH_BUCKET_UNIFORM:
456
506
        size = sizeof(crush_bucket_uniform);
457
507
        break;
464
514
      case CRUSH_BUCKET_STRAW:
465
515
        size = sizeof(crush_bucket_straw);
466
516
        break;
 
517
      default: {
 
518
        char str[128];
 
519
        snprintf(str, sizeof(str), "unsupported bucket algorithm: %d", alg);
 
520
        throw buffer::malformed_input(str);
 
521
      }
 
522
    }
 
523
    crush_bucket *bucket = (crush_bucket*)calloc(1, size);
 
524
    *bptr = bucket;
 
525
 
 
526
    ::decode(bucket->id, blp);
 
527
    ::decode(bucket->type, blp);
 
528
    ::decode(bucket->alg, blp);
 
529
    ::decode(bucket->hash, blp);
 
530
    ::decode(bucket->weight, blp);
 
531
    ::decode(bucket->size, blp);
 
532
 
 
533
    bucket->items = (__s32*)calloc(1, bucket->size * sizeof(__s32));
 
534
    for (unsigned j = 0; j < bucket->size; ++j) {
 
535
      ::decode(bucket->items[j], blp);
 
536
    }
 
537
 
 
538
    bucket->perm = (__u32*)calloc(1, bucket->size * sizeof(__s32));
 
539
    bucket->perm_n = 0;
 
540
 
 
541
    switch (bucket->alg) {
 
542
      case CRUSH_BUCKET_UNIFORM: {
 
543
        ::decode(((crush_bucket_uniform*)bucket)->item_weight, blp);
 
544
        break;
 
545
      }
 
546
 
 
547
      case CRUSH_BUCKET_LIST: {
 
548
        crush_bucket_list* cbl = (crush_bucket_list*)bucket;
 
549
        cbl->item_weights = (__u32*)calloc(1, bucket->size * sizeof(__u32));
 
550
        cbl->sum_weights = (__u32*)calloc(1, bucket->size * sizeof(__u32));
 
551
 
 
552
        for (unsigned j = 0; j < bucket->size; ++j) {
 
553
          ::decode(cbl->item_weights[j], blp);
 
554
          ::decode(cbl->sum_weights[j], blp);
 
555
        }
 
556
        break;
 
557
      }
 
558
 
 
559
      case CRUSH_BUCKET_TREE: {
 
560
        unsigned num_nodes;
 
561
        crush_bucket_tree* cbt = (crush_bucket_tree*)bucket;
 
562
        ::decode(num_nodes, blp);
 
563
        cbt->num_nodes = num_nodes;
 
564
        cbt->node_weights = (__u32*)calloc(1, num_nodes * sizeof(__u32));
 
565
        for (unsigned j=0; j<num_nodes; j++) {
 
566
          ::decode(cbt->node_weights[j], blp);
 
567
        }
 
568
        break;
 
569
      }
 
570
 
 
571
      case CRUSH_BUCKET_STRAW: {
 
572
        crush_bucket_straw* cbs = (crush_bucket_straw*)bucket;
 
573
        cbs->straws = (__u32*)calloc(1, bucket->size * sizeof(__u32));
 
574
        cbs->item_weights = (__u32*)calloc(1, bucket->size * sizeof(__u32));
 
575
        for (unsigned j = 0; j < bucket->size; ++j) {
 
576
          ::decode(cbs->item_weights[j], blp);
 
577
          ::decode(cbs->straws[j], blp);
 
578
        }
 
579
        break;
 
580
      }
 
581
 
467
582
      default:
 
583
        // We should have handled this case in the first switch statement
468
584
        assert(0);
469
 
      }
470
 
      crush->buckets[i] = (crush_bucket*)malloc(size);
471
 
      memset(crush->buckets[i], 0, size);
472
 
 
473
 
      ::decode(crush->buckets[i]->id, blp);
474
 
      ::decode(crush->buckets[i]->type, blp);
475
 
      ::decode(crush->buckets[i]->alg, blp);
476
 
      ::decode(crush->buckets[i]->hash, blp);
477
 
      ::decode(crush->buckets[i]->weight, blp);
478
 
      ::decode(crush->buckets[i]->size, blp);
479
 
 
480
 
      crush->buckets[i]->items = (__s32*)malloc(sizeof(__s32)*crush->buckets[i]->size);
481
 
      for (unsigned j=0; j<crush->buckets[i]->size; j++)
482
 
        ::decode(crush->buckets[i]->items[j], blp);
483
 
 
484
 
      crush->buckets[i]->perm = (__u32*)malloc(sizeof(__s32)*crush->buckets[i]->size);
485
 
      crush->buckets[i]->perm_n = 0;
486
 
 
487
 
      switch (crush->buckets[i]->alg) {
488
 
      case CRUSH_BUCKET_UNIFORM:
489
 
        ::decode(((crush_bucket_uniform*)crush->buckets[i])->item_weight, blp);
490
 
        break;
491
 
 
492
 
      case CRUSH_BUCKET_LIST:
493
 
        ((crush_bucket_list*)crush->buckets[i])->item_weights =
494
 
          (__u32*)malloc(crush->buckets[i]->size * sizeof(__u32));
495
 
        ((crush_bucket_list*)crush->buckets[i])->sum_weights =
496
 
          (__u32*)malloc(crush->buckets[i]->size * sizeof(__u32));
497
 
 
498
 
        for (unsigned j=0; j<crush->buckets[i]->size; j++) {
499
 
          ::decode(((crush_bucket_list*)crush->buckets[i])->item_weights[j], blp);
500
 
          ::decode(((crush_bucket_list*)crush->buckets[i])->sum_weights[j], blp);
501
 
        }
502
 
        break;
503
 
 
504
 
      case CRUSH_BUCKET_TREE:
505
 
        {
506
 
          ::decode(((crush_bucket_tree*)crush->buckets[i])->num_nodes, blp);
507
 
          unsigned num_nodes = ((crush_bucket_tree*)crush->buckets[i])->num_nodes;
508
 
          ((crush_bucket_tree*)crush->buckets[i])->node_weights =
509
 
            (__u32*)malloc(num_nodes * sizeof(__u32));
510
 
          for (unsigned j=0; j<num_nodes; j++)
511
 
            ::decode(((crush_bucket_tree*)crush->buckets[i])->node_weights[j], blp);
512
 
        }
513
 
        break;
514
 
 
515
 
      case CRUSH_BUCKET_STRAW:
516
 
        ((crush_bucket_straw*)crush->buckets[i])->straws =
517
 
          (__u32*)malloc(crush->buckets[i]->size * sizeof(__u32));
518
 
        ((crush_bucket_straw*)crush->buckets[i])->item_weights =
519
 
          (__u32*)malloc(crush->buckets[i]->size * sizeof(__u32));
520
 
        for (unsigned j=0; j<crush->buckets[i]->size; j++) {
521
 
          ::decode(((crush_bucket_straw*)crush->buckets[i])->item_weights[j], blp);
522
 
          ::decode(((crush_bucket_straw*)crush->buckets[i])->straws[j], blp);
523
 
        }
524
 
        break;
525
 
      }
526
 
    }
527
 
 
528
 
    // rules
529
 
    crush->rules = (crush_rule**)malloc(sizeof(crush_rule*)*crush->max_rules);
530
 
    for (unsigned i=0; i<crush->max_rules; i++) {
531
 
      __u32 yes;
532
 
      ::decode(yes, blp);
533
 
      if (!yes) {
534
 
        crush->rules[i] = 0;
535
 
        continue;
536
 
      }
537
 
 
538
 
      __u32 len;
539
 
      ::decode(len, blp);
540
 
      crush->rules[i] = (crush_rule*)malloc(crush_rule_size(len));
541
 
      crush->rules[i]->len = len;
542
 
      ::decode(crush->rules[i]->mask, blp);
543
 
      for (unsigned j=0; j<crush->rules[i]->len; j++)
544
 
        ::decode(crush->rules[i]->steps[j], blp);
545
 
    }
546
 
 
547
 
    // name info
548
 
    ::decode(type_map, blp);
549
 
    ::decode(name_map, blp);
550
 
    ::decode(rule_name_map, blp);
551
 
    build_rmaps();
552
 
 
553
 
    finalize();
 
585
        break;
 
586
    }
554
587
  }
555
588
};
556
589